In JavaScript, when do/don't Dates get converted? [duplicate] - javascript

This question already has answers here:
different result for yyyy-mm-dd and yyyy/mm/dd in javascript when passed to "new Date" [duplicate]
(2 answers)
Closed 6 years ago.
I'm confused because typing the same date in a different format results in two different date outputs, the first converted and the second, not. Here is the code:
var x = new Date("2015-03-25"); // outputs Tue Mar 24 2015 17:00:00 GMT-0700 (PDT)
var y = new Date("03/25/2015"); // outputs Wed Mar 25 2015 00:00:00 GMT-0700 (PDT)

The way dates are parsed by browsers is a huge pile of unpredictable inconsistency. You should not attempt it. Here's the full rundown in case you're curious: http://dygraphs.com/date-formats.html
If you want consistent parsing you should implement it yourself or us a library that does it. Momentjs is widely used: http://momentjs.com/

Related

Simplify writing a date [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I would like to simplify the display of a date (2016-06-26 05:23:55) which on discord.js gives this:
Sun Jun 26 2016 05:23:55 GMT + 0200 (Central European Daylight Time)
I would like to have this : Saturday June 26, 2016 at 05:23
I'd suggest using a library such as date-fns to format the date.
have a look at their docs: https://date-fns.org/v2.16.1/docs/format
You don't have to, but you'll have to it manually (array of months, pick Xth month, ...)

JS Date YYYY-MM-DD Returning incorrect value [duplicate]

This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
I have HTML Date inputs that return a string in the format of 'YYYY-MM-DD'
I then want to put this into the Date Constructor so that I can get it converted into ISOFormat (for use of Mongo Querying)
However, the results are unexpected.
Doing new Date('2020-06-25') returns "Wed Jun 24 2020 20:00:00 GMT-0400 (Eastern Daylight Time)". Note that this is the day prior. Why does this happen and how can I change this to get the current date? THanks
When you create a new Date object in JavaScript, it expects the argument to be in GMT. It then converts it to your local timezone GMT-0400, which explains why its 4 hours off. You can set the timezone by appending it to the date string: new Date('2020-06-25 GMT-0400') should give you "Thu Jun 25 2020 00:00:00 GMT-0400".

Parsing historical UTC date times gives an odd time zone shift [duplicate]

This question already has answers here:
Browsers, time zones, Chrome 67 Error (historic timezone changes)
(2 answers)
weird seconds offset in js date object in chrome
(2 answers)
Wrong minutes and seconds in a JavaScript date before year 1925 [duplicate]
(1 answer)
Closed 3 years ago.
From a rest application I get different times in UTC.
A few examples 2999-01-30T23:00:00.000Z or 1699-12-30T23:00:00.000Z.
I convert it to Time on the front end, using new Date(date) in JavaScript.
The problem is then in the resulting format.
new Date("2999-01-30T23:00:00.000Z") results in Thu Jan 31 2999 00:00:00 GMT+0100, as expected.
But new Date("1699-12-30T23:00:00.000Z") results in Wed Dec 30 1699 23:57:44 GMT+0057.
Why does it suddenly give me this output? Why is there this odd time shift?
I wonder what the problem is, whether anything exists to solve this? I could not find anything.
I'm guessing with a date from 1699, you are back in history where timezones were different. For example here in Denmark we switched to CET in 1894, so I get:
new Date("1893-12-31T00:00:00.000Z");
// Sun Dec 31 1893 00:50:20 GMT+0050 (Central European Standard Time)
new Date("1894-01-01T00:00:00.000Z");
// Mon Jan 01 1894 01:00:00 GMT+0100 (Central European Standard Time)

Constructing a date in a specific timezone [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
How do I specify the time zone when creating a JavaScript Date?
(1 answer)
Closed 5 years ago.
Without using moment.js, is there a way to construct a date in a specific timezone.
For example, I have two strings:
var date = '2018-01-23';
var time = '12:00';
I can construct the date like so:
var constructedDate = new Date(date.substring(0,4), date.substring(5,7)-1, date.substring(8,10), time.substring(0,2), time.substring(3,5));
which provides output of:
Tue Jan 23 12:00:00 GMT+00:00 2018
However, I am looking for output in a particular timezone (e.g +11:00)
Tue Jan 23 12:00:00 GMT+11:00 2018
Alternatively, is there a way to offset the date -11 hours so when I use the GMT date in my application it will be correct.

Why is date being set one day earlier than expected? [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 6 years ago.
var dateTest = new Date('2015-03-31');
console.log(dateTest);
Result:
Mon Mar 30 2015 20:00:00 GMT-0400 (Eastern Daylight Time)
However I expected this to be March 31. How might I do this?
Because it's setting the date according to UTC, and returning it with your local timezone offset. I'd recommend explicitly specifying the timezone offset, if that's what you need, or adding your timezone offset after the fact.
var d = new Date('2015-03-31T00:00:00-0400');
// or
var d = new Date('2015-03-31');
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
If you want it to be in UTC, you can call the toUTCString() method on it instead, which will give you the date you expect, albeit not in your timezone.

Categories

Resources