首页 > 解决方案 > 从javascript中的特定时区获取当前时间戳

问题描述

我正在尝试从与本地时间无关的特定时区获取时间戳。

我希望来自世界各地的客户看到完全相同的时间戳。这甚至可能吗?我不想要 node.js 中的解决方案,但如果有一个工作库,请包含它。

标签: javascriptjquerytimestampunix-timestamp

解决方案


您可以通过 JavaScript、使用Date对象或使用诸如moment.js 之类的专用库来生成与时区无关的时间戳:

const timestampMilliseconds = (new Date()).getTime();
console.log(timestampMilliseconds);

const timestampSeconds = Math.round((new Date()).getTime() / 1000);
console.log(timestampSeconds);

const timestampSecondsMoment = moment().unix();
console.log(timestampSecondsMoment)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>


推荐阅读