I want to parse date without timezone in JavaScript. I have tried:
new Date(Date.parse("2005-07-08T00:00:00+0000"));
Returns Fri Jul 08 2005 02:00:00 GMT+0200 (Central European Daylight Time)
new Date(Date.parse("2005-07-08 00:00:00 GMT+0000"));
Returns same result
new Date(Date.parse("2005-07-08 00:00:00 GMT-0000"));
Returns same result
I want to parse time:
Without time zone.
Without calling constructor Date.UTC or new Date(year, month, day).
Just simple passing string into Date constructor (without prototype approaches).
I have to product
Date
object, notString
.
The
Date
object itself will contain timezone anyway, and the returned result is the effect of converting it to string in a default way. I.e. you cannot create a date object without timezone. But what you can do is mimic the behavior ofDate
object by creating your own one. This is, however, better to be handed over to libraries like moment.js.(new Date().toString()).replace(/ \w+-\d+ \(.*\)$/,"")
This will have output: Tue Jul 10 2018 19:07:11
(new Date("2005-07-08T11:22:33+0000").toString()).replace(/ \w+-\d+ \(.*\)$/,"")
This will have output: Fri Jul 08 2005 04:22:33
Note: The time returned will depend on your local timezone
I have the same issue. I get a date as a String, for example: '2016-08-25T00:00:00', but I need to have Date object with correct time. To convert String into object, I use getTimezoneOffset:
getTimezoneOffset()
will return ether negative or positive value. This must be subtracted to work in every location in world.This is the solution that I came up with for this problem which works for me.
library used: momentjs with plain javascript Date class.
Step 1. Convert String date to moment object (PS: moment retains the original date and time as long as
toDate()
method is not called):const dateMoment = moment("2005-07-08T11:22:33+0000");
Step 2. Extract
hours
andminutes
values from the previously created moment object:Step 3. Convert moment to Date(PS: this will change the original date based on the timezone of your browser/machine, but don't worry and read step 4.):
Step 4. Manually set the hours and minutes extracted in Step 2.
Step 5.
dateObj
will now have show the original Date without any timezone difference. Even the Daylight time changes won't have any effect on the date object as we are manually setting the original hours and minutes.Hope this helps.
The date is parsed correctly, it's just toString that converts it to your local timezone:
Javascript Date object are timestamps - they merely contain a number of milliseconds since the epoch. There is no timezone info in a Date object. Which calendar date (day, minutes, seconds) this timestamp represents is a matter of the interpretation (one of
to...String
methods).The above example shows that the date is being parsed correctly - that is, it actually contains an amount of milliseconds corresponding to "2005-07-08T11:22:33" in GMT.
There are some inherent problems with date parsing that are unfortunately not addressed well by default.
-Human readable dates have implicit timezone in them
-There are many widely used date formats around the web that are ambiguous
To solve these problems easy and clean one would need a function like this:
I have searched for this, but found nothing like that!
So I created: https://github.com/zsoltszabo/timestamp-grabber
Enjoy!