react下的时间戳转换

2017-08-30  本文已影响0人  迷糊的呆呆

在React下面,时间戳和时间字符串的转换的方法中的getYear()等方法失灵,可采取这种转换方式。要改格式的话只要return里面的格式相对应改下就可以了。

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');

}

上一篇下一篇

猜你喜欢

热点阅读