简体   繁体   English

在 Javascript 中创建时区独立日期?

[英]Creating timezone independent dates in Javascript?

This question is related to this question and this question .这个问题与这个问题这个问题有关

So if we construct a date using an ISO string like this:因此,如果我们使用这样的 ISO 字符串构造日期:

new Date("2000-01-01")

Depending on what timezone we are in, we might get a different year and day.根据我们所处的时区,我们可能会得到不同的年份和日期。

I need to be able to constructor dates in Javascript that that always have the correct year , day , and month indicated in a string like 2000-01-01 , and based on the answer in one of the questions if we use back slashes instead like this:我需要能够在 Javascript 中构造日期,该日期始终具有正确的yeardaymonth ,如2000-01-01之类的字符串中指示,并且基于其中一个问题的答案,如果我们使用反斜杠而不是像这个:

const d = new Date("2000/01/01")

Then we will always get the right year , day , and month when using the corresponding date API methods like this:然后,当使用相应的日期 API 方法时,我们将始终得到正确的yeardaymonth ,如下所示:

d2.getDate();
d2.getDay();
d2.getMonth();
d2.getFullYear();

So I just wanted to verify that my understanding is correct?所以我只是想验证我的理解是否正确?

Ultimately I need to be able to create Date instances like this for example:最终,我需要能够创建这样的Date实例,例如:

const d3 = new Date('2010/01/01');
d3.setHours(0, 0, 0, 0);

And the time components should always be zero, and the year , month , and day should be the numbers specified in the string.并且时间分量应始终为零, yearmonthday应为字符串中指定的数字。

Thoughts?想法?

I just did a quick test with this: https://stackblitz.com/edit/typescript-eztrai我刚刚对此进行了快速测试: https://stackblitz.com/edit/typescript-eztrai

const date = new Date('2000/01/01');
console.log(`The day is ${date.getDate()}`);
const date1 = new Date('2000-01-01');
console.log(`The day is ${date1.getDate()}`);

And it logs this:它记录了这个:

The day is 1
The day is 31

So it seems like using backslashes should work...所以看起来使用反斜杠应该可以工作......

Or perhaps using the year, month (0 based index), and day constructor values like this:或者可能使用年、月(基于 0 的索引)和日构造函数值,如下所示:

const date3 = new Date(2000, 0, 1);
date3.setHours(0, 0, 0, 0);
console.log(`The day is ${date3.getDate()}`);
console.log(`The date string is ${date3.toDateString()}`);
console.log(`The ISO string is ${date3.toISOString()}`);
console.log(`Get month ${date3.getMonth()} `);
console.log(`Get year ${date3.getFullYear()} `);
console.log(`Get day ${date3.getDate()} `);

Perhaps you want to take a look at Date.UTC也许你想看看Date.UTC

new Date(Date.UTC(2000, 0, 1))

If you want to later set the date to something different, use an equivalent UTC method (eg setUTCHours ).如果您想稍后将日期设置为不同的日期,请使用等效的 UTC 方法(例如setUTCHours )。

When you retrieve the date, also make sure to use the UTC methods (eg getUTCMonth ).当您检索日期时,还要确保使用 UTC 方法(例如getUTCMonth )。

If you need to pass in an ISO string make sure you include the time offset of +00:00 (is often abbreviated with z )如果您需要传入 ISO 字符串,请确保包含+00:00的时间偏移量(通常缩写为z

new Date("2000-01-01T00:00:00Z");

If you want to retrieve the date in a specific format you can take a look at Intl.DatTimeFormat , just remember to pass in timeZone: "UTC" to the options .如果您想以特定格式检索日期,您可以查看Intl.DatTimeFormat ,只需记住将timeZone: "UTC"传递给options

 const date = new Date("2000-01-01T00:00:00Z"); const dateTimeFormat = new Intl.DateTimeFormat("en-GB", { timeZone: "UTC" }); console.log(dateTimeFormat.format(date));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM