JavaScript 中的日期和时间格式化,类似 .NET C# 或 VB.NET






4.81/5 (24投票s)
JavaScript 中自定义日期和时间格式化,类似 .NET C# 或 VB.NET
引言
日期和时间格式化 字符串
定义了 Date()
对象的文本表示形式,从而生成自定义格式 字符串
。任何不是标准日期和时间格式 字符串
的 字符串
都将被解释为自定义日期和时间格式 字符串
。
您可以下载 format.date
JavaScript 工具,这是一款应用程序,可让您将格式 字符串
应用于日期和时间值,并以 字符串
格式显示结果。
背景
在格式化操作中,自定义日期和时间格式 字符串
可以与 Date()
实例的 format 方法一起使用。
format 方法要求输入字符串完全符合特定的模式才能成功解析,否则会出错(例如:错误:不支持的方法 [方法名称])。
下表描述了自定义日期和时间格式,并显示了每个格式产生的 字符串
结果。
格式 | 描述 | 日期示例 | 输出 |
"d" | 月份中的日期,从 1 到 31。 | 5/1/2014 1:45:30 PM | 1 |
"dd" | 月份中的日期,从 01 到 31。 | 5/1/2014 1:45:30 PM | 01 |
"ddd" | 星期的缩写名称。 | 5/15/2014 1:45:30 PM | 周四 |
"dddd" | 星期中的完整名称。 | 5/15/2014 1:45:30 PM | 星期四 |
"f" | 日期和时间值中的十分之一秒。 | 5/15/2014 13:45:30.617 | 6 |
"ff" | 日期和时间值中的百分之一秒 | 5/15/2014 13:45:30.617 | 61 |
"fff" | 日期和时间值中的毫秒。 | 5/15/2014 13:45:30.617 | 617 |
"F" | 如果非零,则为日期和时间值中的十分之一秒。 | 5/15/2014 13:45:30.617 | 6 |
"FF" | 如果非零,则为日期和时间值中的百分之一秒。 | 5/15/2014 13:45:30.617 | 61 |
"FFF" | 如果非零,则为日期和时间值中的毫秒。 | 5/15/2014 13:45:30.617 | 617 |
"h" | 小时,使用 12 小时制,从 1 到 12。 | 5/15/2014 1:45:30 AM | 1 |
"hh" | 小时,使用 12 小时制,从 01 到 12。 | 5/15/2014 1:45:30 AM | 01 |
"H" | 小时,使用 24 小时制,从 0 到 23。 | 5/15/2014 1:45:30 AM | 1 |
"HH" | 小时,使用 24 小时制,从 00 到 23。 | 5/15/2014 1:45:30 AM | 01 |
"m" | 分钟,从 0 到 59。 | 5/15/2014 1:09:30 AM | 9 |
"mm" | 分钟,从 00 到 59。 | 5/15/2014 1:09:30 AM | 09 |
"M" | 月份,从 1 到 12。 | 5/15/2014 1:45:30 PM | 6 |
"MM" | 月份,从 01 到 12。 | 5/15/2014 1:45:30 PM | 06 |
"MMM" | 月份的缩写名称。 | 6/15/2014 1:45:30 PM | 六月 |
"MMMM" | 月份的完整名称。 | 6/15/2014 1:45:30 PM | 六月 |
"s" | 秒,从 0 到 59。 | 5/15/2014 1:45:09 PM | 9 |
"ss" | 秒,从 00 到 59。 | 5/15/2014 1:45:09 PM | 09 |
"t" | AM/PM 标记的首个字符。 | 5/15/2014 1:45:09 PM | P |
"tt" | AM/PM 标记。 | 5/15/2014 1:45:09 PM | PM |
"y" | 年份,从 0 到 99。 | 5/15/2014 1:45:09 PM | 9 |
"yy" | 年份,从 00 到 99。 | 5/15/2014 1:45:09 PM | 09 |
"yyy" | 年份,至少有三位数。 | 5/15/2009 1:45:30 PM | 2009 |
"yyyy" | 年份,四位数。 | 5/15/2009 1:45:30 PM | 2009 |
"yyyyy" | 年份,五位数。 | 5/15/2009 1:45:30 PM | 02009 |
"z" | 与 UTC 的小时偏移量,无前导零。 | 5/15/2014 1:45:30 PM -07:00 | -7 |
"zz" | 与 UTC 的小时偏移量,单位数时有前导零。 | 5/15/2014 1:45:30 PM -07:00 | -07 |
"zzz" | 与 UTC 的小时和分钟偏移量。 | 5/15/2014 1:45:30 PM -07:00 | -07:00 |
"st" | 日期的序数(st, nd, rd 和 th)显示。 | 5/15/2014 1:45:30 PM | 15日 |
Using the Code
var dayNames = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.format = function (format) {
var wordSplitter = /\W+/, _date = this;
this.Date = function (format) {
var words = format.split(wordSplitter);
words.forEach(function(w) {
if (typeof(wordReplacer[w]) === "function") {
format = format.replace(w, wordReplacer[w]() );
}
else {
wordReplacer['e'](w);
}
});
return format.replace(/\s+(?=\b(?:st|nd|rd|th)\b)/g, "");
};
var wordReplacer = {
//The day of the month, from 1 through 31. (eg. 5/1/2014 1:45:30 PM, Output: 1)
d : function() {
return _date.getDate();
},
//The day of the month, from 01 through 31. (eg. 5/1/2014 1:45:30 PM, Output: 01)
dd : function() {
return _pad(_date.getDate(),2);
},
//The abbreviated name of the day of the week. (eg. 5/15/2014 1:45:30 PM, Output: Mon)
ddd : function() {
return dayNames[_date.getDay()].slice(0,3);
},
//The full name of the day of the week. (eg. 5/15/2014 1:45:30 PM, Output: Monday)
dddd : function() {
return dayNames[_date.getDay()] + 'day';
},
//The tenths of a second in a date and time value. (eg. 5/15/2014 13:45:30.617, Output: 6)
f : function() {
return parseInt(_date.getMilliseconds() / 100) ;
},
//The hundredths of a second in a date and time value.
//(e.g., 5/15/2014 13:45:30.617, Output: 61)
ff : function() {
return parseInt(_date.getMilliseconds() / 10) ;
},
//The milliseconds in a date and time value. (eg. 5/15/2014 13:45:30.617, Output: 617)
fff : function() {
return _date.getMilliseconds() ;
},
//If non-zero, The tenths of a second in a date and time value.
//(eg. 5/15/2014 13:45:30.617, Output: 6)
F : function() {
return (_date.getMilliseconds() / 100 > 0) ? parseInt(_date.getMilliseconds() / 100) : '' ;
},
//If non-zero, The hundredths of a second in a date and time value.
//(e.g., 5/15/2014 13:45:30.617, Output: 61)
FF : function() {
return (_date.getMilliseconds() / 10 > 0) ? parseInt(_date.getMilliseconds() / 10) : '' ;
},
//If non-zero, The milliseconds in a date and time value.
//(eg. 5/15/2014 13:45:30.617, Output: 617)
FFF : function() {
return (_date.getMilliseconds() > 0) ? _date.getMilliseconds() : '' ;
},
//The hour, using a 12-hour clock from 1 to 12. (eg. 5/15/2014 1:45:30 AM, Output: 1)
h : function() {
return _date.getHours() % 12 || 12;
},
//The hour, using a 12-hour clock from 01 to 12. (eg. 5/15/2014 1:45:30 AM, Output: 01)
hh : function() {
return _pad(_date.getHours() % 12 || 12, 2);
},
//The hour, using a 24-hour clock from 0 to 23. (eg. 5/15/2014 1:45:30 AM, Output: 1)
H : function() {
return _date.getHours();
},
//The hour, using a 24-hour clock from 00 to 23. (eg. 5/15/2014 1:45:30 AM, Output: 01)
HH : function() {
return _pad(_date.getHours(),2);
},
//The minute, from 0 through 59. (eg. 5/15/2014 1:09:30 AM, Output: 9
m : function() {
return _date.getMinutes()();
},
//The minute, from 00 through 59. (eg. 5/15/2014 1:09:30 AM, Output: 09
mm : function() {
return _pad(_date.getMinutes(),2);
},
//The month, from 1 through 12. (eg. 5/15/2014 1:45:30 PM, Output: 6
M : function() {
return _date.getMonth() + 1;
},
//The month, from 01 through 12. (eg. 5/15/2014 1:45:30 PM, Output: 06
MM : function() {
return _pad(_date.getMonth() + 1,2);
},
//The abbreviated name of the month. (eg. 5/15/2014 1:45:30 PM, Output: Jun
MMM : function() {
return monthNames[_date.getMonth()].slice(0, 3);
},
//The full name of the month. (eg. 5/15/2014 1:45:30 PM, Output: June)
MMMM : function() {
return monthNames[_date.getMonth()];
},
//The second, from 0 through 59. (eg. 5/15/2014 1:45:09 PM, Output: 9)
s : function() {
return _date.getSeconds();
},
//The second, from 00 through 59. (eg. 5/15/2014 1:45:09 PM, Output: 09)
ss : function() {
return _pad(_date.getSeconds(),2);
},
//The first character of the AM/PM designator. (eg. 5/15/2014 1:45:30 PM, Output: P)
t : function() {
return _date.getHours() >= 12 ? 'P' : 'A';
},
//The AM/PM designator. (eg. 5/15/2014 1:45:30 PM, Output: PM)
tt : function() {
return _date.getHours() >= 12 ? 'PM' : 'AM';
},
//The year, from 0 to 99. (eg. 5/15/2014 1:45:30 PM, Output: 9)
y : function() {
return Number(_date.getFullYear().toString().substr(2,2));
},
//The year, from 00 to 99. (eg. 5/15/2014 1:45:30 PM, Output: 09)
yy : function() {
return _pad(_date.getFullYear().toString().substr(2,2),2);
},
//The year, with a minimum of three digits. (eg. 5/15/2014 1:45:30 PM, Output: 2009)
yyy : function() {
var _y = Number(_date.getFullYear().toString().substr(1,2));
return _y > 100 ? _y : _date.getFullYear();
},
//The year as a four-digit number. (eg. 5/15/2014 1:45:30 PM, Output: 2009)
yyyy : function() {
return _date.getFullYear();
},
//The year as a five-digit number. (eg. 5/15/2014 1:45:30 PM, Output: 02009)
yyyyy : function() {
return _pad(_date.getFullYear(),5);
},
//Hours offset from UTC, with no leading zeros. (eg. 5/15/2014 1:45:30 PM -07:00, Output: -7)
z : function() {
return parseInt(_date.getTimezoneOffset() / 60) ; //hourse
},
//Hours offset from UTC, with a leading zero for a single-digit value.
//(e.g., 5/15/2014 1:45:30 PM -07:00, Output: -07)
zz : function() {
var _h = parseInt(_date.getTimezoneOffset() / 60); //hourse
if(_h < 0) _h = '-' + _pad(Math.abs(_h),2);
return _h;
},
//Hours and minutes offset from UTC. (eg. 5/15/2014 1:45:30 PM -07:00, Output: -07:00)
zzz : function() {
var _h = parseInt(_date.getTimezoneOffset() / 60); //hourse
var _m = _date.getTimezoneOffset() - (60 * _h);
var _hm = _pad(_h,2) +':' + _pad(Math.abs(_m),2);
if(_h < 0) _hm = '-' + _pad(Math.abs(_h),2) +':' + _pad(Math.abs(_m),2);
return _hm;
},
//Date ordinal display from day of the date. (eg. 5/15/2014 1:45:30 PM, Output: 15th)
st: function () {
var _day = wordReplacer.d();
return _day < 4 | _day > 20 && ['st', 'nd', 'rd'][_day % 10 - 1] || 'th';
},
e: function (method) {
throw 'ERROR: Not supported method [' + method + ']';
}
};
_pad = function (n, c) {
if ((n = n + '').length < c) {
return new Array((++c) - n.length).join('0') + n;
}
return n;
}
return this.Date(format);
}
在本节中,我将向您展示如何将 Date()
对象与自定义格式结合使用。
"dd" 自定义格式
"dd" 自定义格式字符串表示月份中的日期,为 01 到 31 的数字。
单个数字的日期会以零作为前导。以下示例在自定义格式 字符串
中包含了 "dd
" 自定义格式说明符。
date = new Date();
console.log(date.format('dd st-MMM-yyyy')); //Output: "15th-May-2014"
"dddd" 自定义格式
"dddd" 自定义格式字符串表示星期几的完整名称。
星期几的名称。以下示例在自定义格式 字符串
中包含了 "dddd
" 自定义格式说明符。
date = new Date();
console.log(date.format('dddd, dd/M/yy')); //Output: "Thursday, 15/5/14"
"ff" 自定义格式
"ff" 自定义格式说明符表示秒的小数部分最高有效两位数字;也就是说,它表示日期和时间值中的百分之一秒。
以下示例在自定义格式 字符串
中包含了 "ff
" 自定义格式说明符。
date = new Date();
console.log(date.format('dddd, dd/M/yy hh:mm:ss.ff')); //Output: "Thursday, 15/5/14 04:51:28.49"
"HH" 自定义格式
"HH" 自定义格式说明符(加上任意数量的额外 "H" 说明符)表示小时,为 00 到 23 的数字;也就是说,小时由一个从午夜开始计算的零基 24 小时制表示。单个数字的小时会以零作为前导。
以下示例在自定义格式 字符串
中包含了 "HH
" 自定义格式说明符。
date = new Date();
console.log(date.format('HH:mm:ss')); //Output: "16:54:51"
"tt" 自定义格式
"t" 自定义格式说明符表示 AM/PM 标记的首个字符。
以下示例在自定义格式 字符串
中包含了 "tt
" 自定义格式说明符。
date = new Date();
console.log(date.format('dd-MM-y HH:mm:ss tt')); //Output: "15-05-14 17:00:20 PM"
"st" 自定义格式
"st
" 自定义格式字符串表示日期中的日序数(例如:st, nd, rd 和 th)。
以下示例在自定义格式 字符串
中包含了 "st
" 自定义格式说明符。
date = new Date();
console.log(date.format('ddd, dd st-MMM-yyyy HH:mm:ss tt zz')); //Output:
//"Thu, 15th-May-2014 17:06:37 PM -05"
关注点
使用以上日期和时间格式实用程序,您可以生成成千上万种各种格式的字符串,根据您的喜好或需求。
如果您需要任何帮助,请随时提问。
您的宝贵反馈、评论和建议将不胜感激。