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



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

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

给我24/05/2018 11:13

这是一种方法。

console.log(moment.unix(1527140580).toString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

如果末尾的时区很重要,您可以在此处阅读时区文档以在该字符串中启用它。

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

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

最新更新