美文网首页
react下的时间戳转换

react下的时间戳转换

作者: 迷糊的呆呆 | 来源:发表于2017-08-30 17:55 被阅读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');

}

相关文章

网友评论

      本文标题:react下的时间戳转换

      本文链接:https://www.haomeiwen.com/subject/xtdodxtx.html