教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript 格式化时间日期函数

javascript 格式化时间日期函数

发布时间:2015-08-02   编辑:jiaochengji.com
本文介绍了javascript 格式化日期与时间的函数示例,有关date函数的用法,有需要的朋友做个参考。

javascript中new Date()得到的是一个国际化时间格式的时间值,在使用中文时不方便,需要对javascript中的日期时间进行格式化.
 

复制代码 代码示例:
<script>
Date.prototype.format = function(format)
{
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
} www.jiaochengji.com
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 t=new Date();
document.write("格式化前:"+t+"<br>");
t=new Date().format("yyyy-MM-dd hh:mm:ss");
document.write("格式化后:"+t);
</script>
 

您可能感兴趣的文章:
javascript 格式化时间日期函数代码
javascript日期计算与格式化日期
javascript日期对象格式化为字符串
js按指定格式显示日期时间的代码
javascript 格式化时间日期函数
javascript日期格式化简单例子
javascript格式化时间日期函数举例
php日期函数的简单示例代码
js格式化日期时间数据函数代码
php日期函数问题

[关闭]
~ ~