首页 > 解决方案 > 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?

标签: javascriptnode.jstimezonemomentjsutc

解决方案


问题是您编辑了系统时间而不是系统时区,因此很明显 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>


推荐阅读