Javascript日期工作很奇怪(当将毫秒转换为秒时)

发布于 2025-01-18 05:58:50 字数 562 浏览 2 评论 0原文

我有未来的日期和现在的日期。这两个日期总是在同一天,但只有不同的时间。我的目标是在未来日期到现在日期之间获得秒数的差异,这是我的计时器的倒数值。问题是当我计算时,我的结果不准确。

在我的研究公式中,将毫秒转换为秒的是millis / 1000.0 < / code>,但不返回准确的倒计时结果;

我的代码

let now = (new Date().getTime() / 1000.0);
let futureDate = (new Date('2022-04-01T17:41:47.000Z').getTime() / 1000.0);

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate

我希望解决方案在所有时区上正常工作,并继承本地系统时区而不是将来的日期时区。

任何帮助都将受到很多赞赏。

I have future date and now date. Both of this dates are always in same day but with just different hours. My goal is to get the difference of seconds between the future date and now date as my countdown value for a timer. The problem is when I calculate I'm getting inaccurate results.

In my research formula of converting milliseconds to seconds is millis / 1000.0 but non of this returns accurate countdown result;

My code

let now = (new Date().getTime() / 1000.0);
let futureDate = (new Date('2022-04-01T17:41:47.000Z').getTime() / 1000.0);

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate

I would like the solution to work normal on all timezones and to inherit local system timezone instead of future date timezone.

Any help will be appreciated so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木有鱼丸 2025-01-25 05:58:50

您应该添加系统时区,如下所示:

let date = new Date('2022-04-01T17:41:47.000Z');
let now = new Date().getTime() / 1000.0;
let futureDate = (date.getTime() + date.getTimezoneOffset() * 60000) / 1000.0;

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate
console.log(difference);

You should add the system timezone, like this:

let date = new Date('2022-04-01T17:41:47.000Z');
let now = new Date().getTime() / 1000.0;
let futureDate = (date.getTime() + date.getTimezoneOffset() * 60000) / 1000.0;

let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate
console.log(difference);

一江春梦 2025-01-25 05:58:50

我想检查一下您是否知道“ 0000-00-00-00t00:00:00.000Z”之类的日期格式总是被认为是通用时间(英国时间)。

尝试使用 +HH:mm而不是最后一个Z字符。

例如,如果您在美国东部,那将是“ 2022-04-01T17:41:47.000-05:00”。

I want to check if you know that date formats like "0000-00-00T00:00:00.000Z" are always recognized as universal time (UK time).

Try using +HH:MM instead of the last Z character.

For example, if you are in the US East, it would be "2022-04-01T17:41:47.000-05:00".

栩栩如生 2025-01-25 05:58:50

由于尾随“Z”,表示 +0 偏移量,时间戳“2022-04-01T17:41:47.000Z”将被解析为偏移量 +0(又名 UTC 或 GMT)。现在和那时之间的毫秒数差异是:

let diff = new Date('2022-04-01T17:41:47.000Z') - Date.now();

其中 diff 的负值表示时间戳是过去的。要将差值转换为秒,请将结果除以 1,000。

如果完全相同的时间运行,则无论本地偏移的系统设置如何(当然考虑到时钟精度),diff 的值都将相同。

对于要解析为本地的时间戳,请删除 Z:

let diff = new Date('2022-04-01T17:41:47.000') - Date.now();

但是,这会将时间戳表示的时间瞬间移动本地偏移量,因此将为每个系统返回不同的 diff 值不同的偏移量。考虑到时间戳有偏移量,这似乎不是一件明智的事情,因此旨在表示时间上的单个瞬间,而不是许多不同瞬间之一(有多少个不同的偏移量,如果历史偏移量可能有数百个)包括)取决于主机偏移。

The timestamp '2022-04-01T17:41:47.000Z' will be parsed as offset +0 (aka UTC or GMT) due to the trailing "Z", denoting a +0 offset. The difference between now and then in milliseconds is:

let diff = new Date('2022-04-01T17:41:47.000Z') - Date.now();

where a negative value for diff means the timestamp is in the past. To convert the difference to seconds, divide the result by 1,000.

If run at exactly the same time, the value for diff will be the same regardless of system settings for local offset (allowing for clock accuracy of course).

For the timestamp to be parsed as local, remove the Z:

let diff = new Date('2022-04-01T17:41:47.000') - Date.now();

However, that shifts the instant in time represented by the timestamp by the local offset, so will return a different value for diff for each system with a different offset. That doesn't seem like a sensible thing to do given the timestamp has an offset and so is intended to represent a single instant in time, not one of many different instants (as many as there are different offsets, perhaps hundreds if historical offsets are included) depending on the host offset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文