日期定义格式化函数
2005-10-28 15:04
405 查看
此代码转自CSDN梅花雪
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
}
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;
};
示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
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
}
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;
};
示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
相关文章推荐
- MySQL的Data_ADD函数与日期格式化函数说明
- JavaScript实现的一个日期格式化函数分享
- Sql 日期格式化函数大全
- 日期格式化函数
- js时间日期格式化封装函数
- 格式化为时间日期的函数
- js日期格式化函数
- ASP格式化日期的函数
- Sql Server 日期格式化函数
- SQL Server CONVERT() 函数 -----SqlServer格式化日期
- strftime()函数根据区域设置格式化本地时间/日期 setlocale() 函数设置地区信息(地域信息)
- 用户定义的日期/时间格式(Format 函数)
- 日期格式化函数
- js日期格式化函数
- 一个javascript日期格式化函数
- 日期格式化函数
- Sql Server 中一个非常强大的日期格式化函数
- sql常用函数和日期格式化
- 一个js日期格式化函数
- js 时间戳 日期格式化 函数