Javascript Date constructor odd behavior [duplicate] - javascript

This question already has answers here:
Ambiguous behavior of javascript new Date() constructor for same different formatted string
(3 answers)
Closed 2 months ago.
I am getting an odd behavior if I pass the Date constructor just a date string. Example:
<div>Date 1: <span id="mydate1"></span></div>
<div>Date 2: <span id="mydate2"></span></div>
<div>Date 3: <span id="mydate3"></span></div>
<script>
document.getElementById("mydate1").innerHTML = (new Date("2022-06-28 20:32:00")).toString();
document.getElementById("mydate2").innerHTML = (new Date("2022-06-28")).toString();
document.getElementById("mydate3").innerHTML = (new Date("2022-06-28 00:00:00")).toString();
</script>
This results in the following:
Date 1: Tue Jun 28 2022 20:32:00 GMT-0400 (Eastern Daylight Time)
Date 2: Mon Jun 27 2022 20:00:00 GMT-0400 (Eastern Daylight Time)
Date 3: Tue Jun 28 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
Date 1 works as I would expect. But why are Date 2 and 3 different? With just a date string, it seems to be inferring the date is in UTC, where as with a time field it seems to assume it is in my locale time.
Any thoughts? I can work around it by appending the 00:00:00 text.

It is explained in the documentation of Date constructor, when the argument is a date string:
Date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local.

Related

Transform format of date [duplicate]

This question already has answers here:
How to get date in UTC format irrespective of current system date with javascript?
(4 answers)
Closed 2 years ago.
I have following date Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas). I need to compare this date with the date that is stored in API. The problem is that the date in API is in this format 2020-06-27T12:34:00.000Z.
Is there any way I can transform the format of the first date to be the same as the second date? Or can I comprare them in some way, that the formats wont matter?
Thank you in advance.
Use Date.prototype.toISOString()
The toISOString() method returns a string in simplified extended ISO format (ISO 8601),
which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).
The timezone is always zero UTC offset, as denoted by the suffix "Z".
const time = 'Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)'
const result = new Date(time).toISOString();
console.log(result);
You can do this
const date = new Date("Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)");
const formattedDate = date.toISOString(); // This the formatted date

`new Date()` parsing is format dependent [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I was surprised to realize that
new Date('2015-02-15') and new Date('02-15-2015') return different results. One parses the date as midnight UTC time, the other is parsed as midnight local time. This is also true of the moment-timezone package.
Is there a way to parse these dates in a standard way, either using moment or the builtin Date?
Passing expected date string format to moment.js seem to return the same local timezone
// Sun Feb 15 2015 00:00:00 GMT-0800
moment('2015-02-15', 'YYYY-MM-DD').toString()
// Sun Feb 15 2015 00:00:00 GMT-0800
moment('02-15-2015', 'MM-DD-YYYY').toString()
You can use moment-timezone package for standardizing the time into UTC for example
// Sun Feb 15 2015 08:00:00 GMT+0000
moment('2015-02-15', 'YYYY-MM-DD').utc().toString()
// Sun Feb 15 2015 08:00:00 GMT+0000
moment('02-15-2015', 'MM-DD-YYYY').utc()toString()
Try it out: https://jsfiddle.net/wyopm3xu/

js Date changes on october 10th (BST) [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
Why does JS Date object change toUTCString on October 10th?
new Date('2017-10-9').toUTCString()
"Sun, 08 Oct 2017 23:00:00 GMT"
new Date('2017-10-10').toUTCString()
"Tue, 10 Oct 2017 00:00:00 GMT"
I am writing these in the UK. BST ends on October 29th. What is going on?!
In the first example the date is parsed as local date, and in the second as UTC date. To parse the first date as UTC too, add a 0 before 9.
console.log(new Date('2017-10-09').toUTCString()); // Mon, 09 Oct 2017 00:00:00 GMT
Inconsistencies in date parsing like that are why you should always pass a date in ISO-8601 format to the Date constructor. You can also use a library like Moment.js.

Reformat a date string [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I am working towards formatting my date and time from MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME) to look like MON APR 08 2013 01:01:01. Although I am having no luck with everything I have tried. Can someone shed a little light. Below is the last piece of code I have tried. Thanks.
var date = new Date(parseInt(data[0].published.substr(6)));
var time = new Date(date.toLocaleDateString());
If you can, the best practice would probably be to format the date server-side, or at least present a more universally useful date (like a UNIX timestamp) instead of the formatted string.
However, if changing the server-side output is not an option, you can use the javascript date object. I see you've tried that, but you're not using the date object's constructor properly:
var dateString = 'MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME)';
var dte = new Date(dateString);
document.write(dte.toDateString()); // output: Mon Apr 08 2013
Try it: http://jsfiddle.net/BvLkq/
If you need to reconstruct the time, you can use toLocaleDateString (docs) to pass a locale or format string, or you can build one up by hand using the getHours() (etc) functions .
Documentation
Date object on MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Just use a simple regex.
var str = 'MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME)';
console.log(str.replace(/(.*\d{2}\:\d{2}\:\d{2}).*$/, '$1'));
// outputs MON APR 08 2013 00:00:00

new Data in javascript returns different date [duplicate]

This question already has an answer here:
javascript Date why is the Date new Date("2011-12-13") considered a monday and not a tuesday?
(1 answer)
Closed 9 years ago.
Input: new Date("2013-03-28")
Output: Wed Mar 27 2013 17:00:00 GMT-0700 (PDT)
How do I get 28 instead of 27. Is this a javascript default issue?
When using ISO-formatted dates, either all or in-part, the timezone may be assumed to be UTC.
console.log(new Date("2013-03-28").toUTCString());
// "Thu, 28 Mar 2013 00:00:00 GMT"
To create the date in local time, you can use a different overload of the constructor (note that month is 0-indexed, so 2 is March):
console.log(new Date(2013, 2, 28).toString());
// "Thu Mar 28 2013 00:00:00 GMT-0700 (...)"

Categories