typescript - Creating timezone independent dates in Javascript? - Stack Overflow

This question is related to this question.So if we construct a date using an ISO string like this:new

This question is related to this question.

So if we construct a date using an ISO string like this:

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 construct 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:

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:

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:

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

And the time ponents should always be zero, and the year, month, and day should be the numbers specified in the string.

Thoughts?

I just did a quick test with this:

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:

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()} `);

NOTE

Runar mentioned something really important in the accepted answer ments. To get consistent results when using the Javascript Date API use methods like getUTCDate(). Which will give us 1 if the date string is 2000-01-01. The getDate() method could give us a different number ...

This question is related to this question.

So if we construct a date using an ISO string like this:

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 construct 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:

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:

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:

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

And the time ponents should always be zero, and the year, month, and day should be the numbers specified in the string.

Thoughts?

I just did a quick test with this: https://stackblitz./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:

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()} `);

NOTE

Runar mentioned something really important in the accepted answer ments. To get consistent results when using the Javascript Date API use methods like getUTCDate(). Which will give us 1 if the date string is 2000-01-01. The getDate() method could give us a different number ...

Share Improve this question edited Jan 4, 2022 at 22:03 Ole asked Dec 25, 2021 at 16:27 OleOle 47.5k70 gold badges237 silver badges445 bronze badges 13
  • So you want the Date object to be created in the users current timezone (instead of UTC as in the first snippet)? Couldn't you just attach a "T00:00" to it to force it to be parsed that way? – UnholySheep Commented Dec 25, 2021 at 16:31
  • I guess you want (new Date("2000-01-01")).toISOString() – digitalniweb Commented Dec 25, 2021 at 16:32
  • 1 If you want to store just the day, month, and year without timezone or time, I wouldn't use a Date. Just make a custom object. – skara9 Commented Dec 25, 2021 at 16:34
  • Well ... One of the questions said that if we use 2000/01/01, then we always will get the right year, month, and day from the Javascript API ... so just wanted to confirm that this is the case ... – Ole Commented Dec 25, 2021 at 16:36
  • 1 Everything seems well explained in this impressive article codeofmatt./javascript-date-parsing-changes-in-es6 and yes, a library like moment.js makes it quite trivial: moment("yyyy-mm-dd") (local) vs moment.utc("yyyy-mm-dd") (UTC) – Roko C. Buljan Commented Dec 25, 2021 at 16:42
 |  Show 8 more ments

1 Answer 1

Reset to default 7

From the ECMA standard of the Date.parse method:

When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

What is happening is that New Date() implicitly calls Date.parse on the string. The "2000-01-01" version conforms to a Date Time String Format with a missing offset representation, so it is assumed you mean UTC.

When you use "2000/01/01" as input the standard has this to say:

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

So in short the browser can do what they want. And in your case it assumes you mean the offset of the local time, so whichever offset you are located in gets added when you convert to UTC.

For consistent results, perhaps you want to take a look at Date.UTC

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

If you need to pass in an ISO string make sure you include the time offset of +00:00 (is often abbreviated with z)

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

If you want to later set the date to something different, use an equivalent UTC setter method (e.g. setUTCHours).

When you retrieve the date, also make sure to use the UTC getter methods (e.g. getUTCMonth).

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

console.log(date.getUTCDate());
console.log(date.getUTCMonth());
console.log(date.getUTCFullYear());

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.

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

console.log(dateTimeFormat.format(date));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744242787a4564784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信