本文实例讲述了javascript实现时间格式输出formatdate函数。分享给大家供大家参考。具体如下:
javascript没有提供像fmt标签一样对日期时间型内容格式输入的函数:
下面是我下的时间输出函数,使用时直接放到标签内,调用。代码如下
复制代码 代码如下:
date.prototype.format = function(fmt) { //author: meizz
if (this == invalid date) {
return ;
}
var o = {
m+ : this.getmonth() + 1, //月份
d+ : this.getdate(), //日
h+ : this.gethours(), //小时
m+ : this.getminutes(), //分
s+ : this.getseconds(), //秒
q+ : math.floor((this.getmonth() + 3) / 3), //季度
s : this.getmilliseconds()
//毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(regexp.$1, (this.getfullyear() + )
.substr(4 - regexp.$1.length));
for ( var k in o)
if (new regexp(( + k + )).test(fmt))
fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k])
: ((00 + o[k]).substr(( + o[k]).length)));
return fmt;
}
使用时直接用
复制代码 代码如下:
new date( 时间变量 ).format(yyyy-mm-dd hh:mm:ss)
希望本文所述对大家的javascript程序设计有所帮助。