例如:
现有一字符串为:
var dt="2010-1-1 12:20:20";
第一步:将其转换成日期时间型数据
var newDt=new Date(dt.replace("-","/")); 第二步:格式化数据成"yyyy-MM-dd"
(1)扩展Date
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., (this.getFullYear() "").substr(4 - RegExp..length));
}
for(var k in o) {
if(new RegExp("(" k ")").test(format)) {
format = format.replace(RegExp., RegExp..length==1 ? o[k] : ("00" o[k]).substr(("" o[k]).length));
}
}
return format;
}; (2)使用format格式化时间
var fmtDt=newDt.format("yyyy-MM-dd"); js中处理时间麻烦死人,没有C#来的快,一句OK
var fmtDt=Convert.ToDateTime(dt).ToString("yyyy-MM-dd");