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/
Related
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.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I have a very strange behavior in my web app. On one page, new Date() parsing works fine while on other pages, it seems like I get a totally different behavior from the new Date() parsing and it completely fails. I have verified that the Date object has not been overwritten extended or modified and I even tried using moment.js and I get the same results when it's converted to a Date object. I have also verified the same behavior in both Chrome and Edge. Any suggestions?
Page 1 - Seems to work fine, when I type into console.
new Date()
Mon Mar 25 2019 12:58:45 GMT-0400 (Eastern Daylight Time)
new Date("3/25/2019 12:29:51 PM")
Mon Mar 25 2019 12:29:51 GMT-0400 (Eastern Daylight Time)
Page 2 - For some reason parsing fails and returns a completely wrong Date value
new Date()
Mon Mar 25 2019 12:58:52 GMT-0400 (Eastern Daylight Time)
new Date("3/25/2019 12:29:51 PM")
Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)
I also noticed that on page 1, Date.parse("3/25/2019 12:29:51 PM") works but when I try the same on page 2, it returns null.
It seems Page 2 was actually using a script called DateJS (so it was modified) that had some bugs in it too, when I updated the version to the latest, all issues were resolved. Thanks Rayon for your help.
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.
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
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 (...)"