日期字符串转换成日期对象
var ss = "2012/05/09 12:32:56"; new Date(ss);
但是注意,日期字符串格式必须是上面那种
日期对象转成成日期字符串
(new Date()).format("yyyy 年 MM 月 dd 日 hh 时 mm 分 ss 秒")
format函数是扩展的Date对象,代码如下:
Date.prototype.format = function(format)
{
/*
* format="yyyy-MM-dd hh:mm:ss";
*/
var o = {
"M+" : this.getMonth() + 1, // month
"d+" : this.getDate(), // day
"h+" : this.getHours(), // hour
"m+" : this.getMinutes(), // minute
"s+" : this.getSeconds(), // second
"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
"S" : this.getMilliseconds()
// millisecond
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
- RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};
本文介绍如何将日期字符串转换为日期对象以及如何将日期对象转换回日期字符串。通过JavaScript中的Date对象及其扩展format方法,实现精确到秒的日期格式化。
2180

被折叠的 条评论
为什么被折叠?



