<script type="text/javascript">
Date.prototype.format = function (format) {
/*
* eg: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;
}
var date=new Date(); //获取系统时间
//var date=new Date(param); // param 时间的毫秒
var n=date.format("yyyy-MM-dd hh:mm:ss");//调用
alert(n);
</script>js笔记 Date时间格式
最新推荐文章于 2025-09-16 03:05:38 发布
本文介绍了一种在JavaScript中自定义日期格式化的实现方法。通过扩展Date对象的方法,可以轻松地将日期转换为指定格式的字符串,如'yyyy-MM-dd hh:mm:ss'。这种方法简单实用,适用于多种场景。

1529

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



