记录一个通用的根据时间戳格式化时间的js代码:
var dateFormat = function(validDays, fstr, utc) { utc = utc ? 'getUTC' : 'get'; var timestamp=new Date().getTime() + validDays; var date = new Date(timestamp); return fstr.replace(/\%[YmdHMS]/g, function (m) { //alert(m); switch (m) { case '%Y': return date[utc + 'FullYear'](); // no leading zeros required case '%m': m = 1 + date[utc + 'Month'](); break; case '%d': m = date[utc + 'Date'](); break; case '%H': m = date[utc + 'Hours'](); break; case '%M': m = date[utc + 'Minutes'](); break; case '%S': m = date[utc + 'Seconds'](); break; default: return m.slice(1); // unknown code, remove % } //alert(m); // add leading zero if required return ('0' + m).slice(-2); }); }
使用:
dateFormat(tempTime, '%Y.%m.%d')