javascript - 从javascript中的特定时区获取当前时间戳
问题描述
我正在尝试从与本地时间无关的特定时区获取时间戳。
我希望来自世界各地的客户看到完全相同的时间戳。这甚至可能吗?我不想要 node.js 中的解决方案,但如果有一个工作库,请包含它。
解决方案
您可以通过 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>
推荐阅读
- java - 设置 @Value 以使用 @ConfigurationProperties 前缀
- javascript - 用于创建模板组件的 React JS 系统
- java - org.mockito.exceptions.misusing.InjectMocksException:无法实例化@InjectMocks
- python - 如何从管理员更改页面隐藏字段但将其保留在 Django 的管理员添加页面中
- unity3d - 应用程序能否在运行 IIS 的同时通过端口 80 建立 Websocket 连接?
- ssh - 使用 ansible vault 密码作为 ask-become-pass 和 ssh 密码
- assembly - 了解 MASM 汇编器生成的可执行代码
- android - 如何将屏幕宽度分成两部分以进行滑动手势
- flutter - Flutter 本地身份验证自定义
- php - PHP Prophecy ->reveal() 方法 - 我应该将期望(如“willReturn”、“ShouldBeCalled”)放在它之前还是之后?