如何在时刻js中使用时区将纪元时间戳转换为日期时间?

时间:2018-05-07 06:19:10

标签: javascript angular datetime momentjs

如何在时刻js中将纪元时间戳1527140580转换为Thu May 24 2018 11:13:00 GMT+0530 (India Standard Time)

moment.unix(1527140580).format('DD/MM/YYYY HH:mm');

给了我24/05/2018 11:13

2 个答案:

答案 0 :(得分:2)

这是一种方法



console.log(moment.unix(1527140580).toString())

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
&#13;
&#13;
&#13;

如果最后的时区很重要,您可以阅读timezone docs here以在该字符串中启用它。

&#13;
&#13;
var abbrs = {
    IST : 'Indian Standard Time'
};

moment.fn.zoneName = function () {
    var abbr = this.zoneAbbr();
    return abbrs[abbr] || abbr;
};

console.log(moment.tz(moment.unix(1527140580),"Asia/Calcutta").format('ddd MMMM D YYYY HH:mm:ss [GMT]ZZ (zz)'));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

const result = moment(1527140580);
result.toString() // "Sun Jan 18 1970 21:42:20 GMT+0530"