以前做項目都用框架什么的,很少遇見原生js ,在做某個功能的時候,返回前端的時間出現了問題。
經過幾番斗爭,總算是整出來了。
本人親測有效!
需要轉換的時間戳:/Date(1603938259000)/
1 var timestamp = "/Date(1603938259000)/"; 2 var date = new Date(parseInt(timestamp.replace("/Date(", "").replace(")/", ""), 10)); 3 Y = date.getFullYear() + '-'; 4 M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; 5 D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '; 6 h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'; 7 m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':'; 8 s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds()); 9 var NewDtime = Y + M + D + h + m + s;
轉換后的時間:2020-10-29 10:24:19
如若轉載,請備明出處,謝謝!

本文連接:https://www.cnblogs.com/yifeixue/p/13895452.html
