react下的時間戳轉(zhuǎn)換

在React下面,時間戳和時間字符串的轉(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');

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容