当前位置:编程学习 > JS >>

js Date函数的格式化


/*
*日期格式1.2.3
*(三)2007-2009史蒂芬levithan
* mit许可
*
*包括由scott特伦达增强
*和克里斯科沃尔
*
*接受一个日期,一个面具,或一个日期和口罩。
*返回一个给定的日期格式的版本。
*日期默认为当前日期/时间。
*默认为dateformat.masks.default的面具。*/

var dateformat = function () {
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([hhmstt])1?|[llosz]|"[^"]*"|'[^']*'/g,
timezone = /b(?:[pmcea][sdp]t|(?:pacific|mountain|central|eastern|atlantic) (?:standard|daylight|prevailing) time|(?:gmt|utc)(?:[-+]d{4})?)b/g,
timezoneclip = /[^-+da-z]/g,
pad = function (val, len) {
val = string(val);
len = len || 2;
while (val.length < len) val = "0" + val; return val; }; // regexes and supporting functions are cached through closure return function (date, mask, utc) { var df = dateformat; // you can't provide utc if you skip other args (use the "utc:" mask prefix) if (arguments.length == 1 && object.prototype.tostring.call(date) == "[object string]" && !/d/.test(date)) { mask = date; date = undefined; } // passing date through date applies date.parse, if necessary date = date ? new date(date) : new date; if (isnan(date)) throw syntaxerror("invalid date"); mask = string(df.masks[mask] || mask || df.masks["default"]); // allow setting the utc argument via the mask if (mask.slice(0, 4) == "utc:") { mask = mask.slice(4); utc = true; } var _ = utc ? "getutc" : "get", d = date[_ + "date"](), d = date[_ + "day"](), m = date[_ + "month"](), y = date[_ + "fullyear"](), h = date[_ + "hours"](), m = date[_ + "minutes"](), s = date[_ + "seconds"](), l = date[_ + "milliseconds"](), o = utc ? 0 : date.gettimezoneoffset(), flags = { d: d, dd: pad(d), ddd: df.i18n.daynames[d], dddd: df.i18n.daynames[d + 7], m: m + 1, mm: pad(m + 1), mmm: df.i18n.monthnames[m], mmmm: df.i18n.monthnames[m + 12], yy: string(y).slice(2), yyyy: y, h: h % 12 || 12, hh: pad(h % 12 || 12), h: h, hh: pad(h), m: m, mm: pad(m), s: s, ss: pad(s), l: pad(l, 3), l: pad(l > 99 ? math.round(l / 10) : l),
t: h < 12 ? "a" : "p",
tt: h < 12 ? "am" : "pm",
t: h < 12 ? "a" : "p",
tt: h < 12 ? "am" : "pm", z: utc ? "utc" : (string(date).match(timezone) || [""]).pop().replace(timezoneclip, ""), o: (o > 0 ? "-" : "+") + pad(math.floor(math.abs(o) / 60) * 100 + math.abs(o) % 60, 4),
s: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};

return mask.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
}();

// some common format strings
dateformat.masks = {
"default": "ddd mmm dd yyyy hh:mm:ss",
shortdate: "m/d/yy",
mediumdate: "mmm d, yyyy",
longdate: "mmmm d, yyyy",
fulldate: "dddd, mmmm d, yyyy",
shorttime: "h:mm tt",
mediumtime: "h:mm:ss tt",
longtime: "h:mm:ss tt z",
isodate: "yyyy-mm-dd",
isotime: "hh:mm:ss",
isodatetime: "yyyy-mm-dd't'hh:mm:ss",
isoutcdatetime: "utc:yyyy-mm-dd't'hh:mm:ss'z'"
};

// internationalization strings
dateformat.i18n = {
daynames: [
"sun", "mon", "tue", "wed", "thu", "fri", "sat",
"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
],
monthnames: [
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"
]
};

// for convenience...
date.prototype.format = function (mask, utc) {
return dateformat(this, mask, utc);
};

使用方法

 

var now = new date();

now.format("m/dd/yy");

/ /返回,例如,6/09/07

/ /也可作为一个独立的函数中使用
dateformat(现在,“dddd将,嗯局副局长,西元年,高:mm:ss的特”);
/ /周六,2007年6月9日,下午5时46分21秒

/ /你可以使用多种命名口罩1
now.format(“isodatetime”);
/ / 2007 - 06 - 09t17:46:21

/ / ...或添加您自己的
dateformat.masks.hammertime ='为hh:mm! “可以'吨触摸这个!”;
now.format(“hammertime”);
/ / 17:46!无法触摸这个!

/ /当使用独立dateformat功能,
/ /你也可以提供一个字符串日期
dateformat(“2007年6月9日”,“fulldate”);
/ /周六,2007年6月9日

/ /注意,如果您不包括面具的论据,
/ / dateformat.masks.default使用
now.format();
/ /周六2007年6月9日十七点46分21秒

/ /如果你不包含日期参数,
/ /当前日期和时间是用来
dateformat();
/ /周六2007年6月9日17时46分22秒

/ /你也可以跳过date参数(只要你不面具
/ /包含任何数字),在这种情况下,当前日期/时间是用来
dateformat(“长期”);
/ /美国东部时间下午5点46分22秒

/ /最后,你可以转换本地时间为utc时间。无论是通过在
/ /真正作为一个额外的参数(在这种情况下没有理由可以跳过):
dateformat(现称“老”,真);
now.format(“长期”,真实);
/ /这两条线的回报,例如,下午10时46分21秒星期日

补充:网页制作,jquery 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,