在React下面,時(shí)間戳和時(shí)間字符串的轉(zhuǎn)換的方法中的getYear()等方法失靈,可采取這種轉(zhuǎn)換方式。要改格式的話只要return里面的格式相對應(yīng)改下就可以了。
getDateString:function(now){
Date.prototype.format = function(format) {
var date = {
"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+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
};
var date = new Date(now);//注意,毫秒的話就不用乘以1000了,秒才要乘以1000
console.log(date.format('yyyy.MM.dd hh:mm'));
return date.format('yyyy.MM.dd hh:mm');
}