0

I have this string which I want to create a date from :

Wed Jun 16 2021 00:00:00 GMT+0300 (Israel Daylight Time)

Now when I try to make a new date from it :

const date = new Date('Wed Jun 16 2021 00:00:00 GMT+0300 (Israel Daylight Time)');

And try to output it, it shows :

2021-06-15T21:00:00.000Z

Now, the problem is that it moves the date by one day. I've read few threads that are opened with similar problem to mine saying it is something with the conversion that JSON does and such. I tried multiple things that will show the date correctly ( should be 16 and not 15 ), but without luck.

How do I make a date that is showing the correct date without any conversions ?

1
  • The two timestamps are equivalent. The built–in parser converts the input timestamp to its UTC equivalent, represented as a time value that is an offset from 1970-01-01T00:00:00Z. That can then be represented in any timezone for any date, e.g. using toLocaleString with the timeZone option, e.g. new Date().toLocaleString('default',{timeZone:'Asia/Jerusalem', timeZoneName:'long'}) produces something like "03/06/2021, 14:58:02 Israel Daylight Time". Commented Jun 3, 2021 at 11:56

2 Answers 2

3

It has to do something with Timezone. Check out https://en.wikipedia.org/wiki/ISO_8601.

Your time is 0:00 of 16, after adjusting to the timezone that is UTC, the time is 21:00 of 15. There is a 3 hours difference because of GMT+0300.

const date = new Date('Wed Jun 16 2021 00:00:00 GMT+0300 (Israel Daylight Time)');
console.log(date.toLocaleString('en-US', { timeZone: 'Asia/Jerusalem' }));

List of timezones https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Sign up to request clarification or add additional context in comments.

1 Comment

of course it it something with timezones. But how to eliminate \ fix it ?
1

To me this works

//parse as ms - sidenote: I used midday (12:00) instead of midnight (00:00) to show more clearly, otherweise the day would back to the 15th
const dateMs = Date.parse('Wed Jun 16 2021 12:00:00 GMT+0300 (Israel Daylight Time)');
//1623834000000 //just to be clearer


//create the new timestamp starting from the timestamp
//Being me in EU, it gives the format in my locale (CET - DST)
const date = new Date(dateMs);
Wed Jun 16 2021 11:00:00 GMT+0200 (European Central Time DST)
// this is correct being me at UTC+2 and you at UTC+3

Note that your code is also correct: you have to factor in the difference between timezones 00:00 in UTC+3 is 21:00 in UTC hence the day before

3 Comments

is there a way to construct a new date so it will hold the same date and timezone passed to it ?
The use of Date.parse is redundant. Date.parse(string) and new Date(string) use the same built–in parser and will produce exactly the same time value (which might be NaN if it can't parse the string). The only difference is that parse returns the time value whereas the Date constructor returns a Date object with the time value as its sole data value.
@Wahalez—Date objects are just a time value, nothing more. The value used to create them (if there was one) is not stored, nor any other information about the source such as language, timezone, offset or format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.