javascript - How can i get utc time in Java Script, which is not dependent on client system?
问题描述
In moment.js
moment().utc()
moment()
In this both case i get Client side time. I have a problem when users change their time. For example if users clock is 2 hour ahead or behind.
I am trying to create a timer in JS which should not be dependent on client side time.
Any solutions?
解决方案
问题是您编辑了系统时间而不是系统时区,因此很明显 utc 时间也发生了变化。
如果您的范围是创建一个计时器,您可以使用moment-timer
. 它是一个持续时间的插件。该脚本独立于系统时间。
这里有文档
这里有一个例子:
console.log('start the timer');
const timer = moment.duration(5, 'seconds').timer({
loop: true
}, function() {
console.log('End timer');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-timer/lib/moment-timer.js"></script>
推荐阅读
- excel - 计算逗号分隔列表中的重复值
- python - 将带有 `\` 的字符串转换为 json 或字典数据
- html - CSS margin-bottom 如果元素不能并排显示
- c++ - 闰秒和 std::chrono
- unity3d - 如何使用 ScriptableObject 进行关卡的预制转换?
- javascript - 尝试隧道 localhost web api,前端框架角度时,serveo.net 返回 502
- javascript - JQuery Multiselect 不适用于 Ajax 调用
- c# - 实现逻辑的最佳方式,因此两个相似的类具有共同和不同的功能
- swift - 子枚举 rawValue
- google-sheets - 扫描列名,选择包含特定文本的列名,并对它们运行函数(SUM、MAX 等)