Javascript timezone date parameter

I want to parse a date without a timezone in JavaScript. I tried:

new Date(Date.parse("2005-07-08T00:00:00+0000")); 

Returns Fri Jul 08 2005 02:00:00 GMT + 0200 (Central European Summer Time)

 new Date(Date.parse("2005-07-08 00:00:00 GMT+0000")); 

Returns the same result

 new Date(Date.parse("2005-07-08 00:00:00 GMT-0000")); 

Returns the same result

I want to analyze the time:

  • No time zone.

  • Without calling the constructor Date.UTC or a new date (year, month, day).

  • Just a simple line in the Date constructor (no prototypes).

  • I should use a Date object, not a String .

+118
javascript date timestamp-with-timezone
Jul 09 '13 at 10:18
source share
8 answers

The date is parsed correctly, it's just toString, which converts it to the local time zone:

 > new Date(Date.parse("2005-07-08T11:22:33+0000")) Fri Jul 08 2005 13:22:33 GMT+0200 (CEST) > new Date(Date.parse("2005-07-08T11:22:33+0000")).toUTCString() "Fri, 08 Jul 2005 11:22:33 GMT" 

The Date Javascript object is a timestamp - it just contains a few milliseconds from an era. The Date object does not contain time zone information. What is the date of the calendar (day, minute, second), this timestamp is a matter of interpretation (one of the methods to...String ).

The above example shows that the date is correctly parsed, that is, it actually contains the number of milliseconds corresponding to "2005-07-08T11: 22: 33" in GMT.

+83
Jul 09 '13 at 10:26
source share

I have the same problem. I get the date as a String, for example: '2016-08-25T00: 00: 00', but I need to have a Date object with the correct time. To convert String to an object, I use getTimezoneOffset:

 var date = new Date('2016-08-25T00:00:00') var userTimezoneOffset = date.getTimezoneOffset() * 60000; new Date(date.getTime() - userTimezoneOffset); 

getTimezoneOffset() returns a negative or positive ether value. It must be deducted to work anywhere in the world.

+88
Aug 29 '16 at 15:25
source share

I ran into the same problem and then remembered something elusive about the project I was working on and how they dealt with this problem. At that time, I did not understand this, and I did not care until I ran into the problem myself.

 var date = '2014-01-02T00:00:00.000Z' date = date.substring(0,10).split('-') date = date[1] + '-' + date[2] + '-' + date[0] new Date(date) #Thu Jan 02 2014 00:00:00 GMT-0600 

For any reason, passing the date as "01-02-2014" sets the time zone to zero and ignores the user's time zone. This may be an accident in the Date class, but it existed some time ago and exists today. And it looks like a cross browser. Try it yourself.

This code is implemented in a global project, where time intervals are of great importance, but the person looking at the date did not care about at what point it was entered.

+12
Jan 04 '15 at 18:17
source share

The Date object itself will contain the time zone, and the return result is converting it to the default string. That is, you cannot create a date object without a time zone. But what you can do is simulate the behavior of a Date object by creating your own. This, however, is best passed to libraries such as moment.js .

+5
Jul 09 '13 at 10:25
source share

a simple solution

 const handler1 = { construct(target, args) { let newDate = new target(...args); var tzDifference = newDate.getTimezoneOffset(); return new target(newDate.getTime() + tzDifference * 60 * 1000); } }; Date = new Proxy(Date, handler1); 
+3
Mar 07 '18 at 16:11
source share

Trying to solve the problem with the time zone in the date object, I succeeded in this parsing function:

 function (itemValue, inputFormat) { var value; if (typeof itemValue === "string") { value = inputFormat ? moment(itemValue, inputFormat, true) : moment(itemValue); if (value.isValid && value.isValid()) { value = value.toDate(); } else { console.error("date convert error for", itemValue, inputFormat); } } else if (typeof itemValue === "object" && itemValue.getTimezoneOffset) value = itemValue; else console.error("Date value must be string or date object"); value.setMinutes(0); value.setSeconds(0); value.setMilliseconds(0); value.setHours(-value.getTimezoneOffset() / 60); console.log(value.toLocaleDateString()); return value; } 

This function gives a date object with the correct clock as the time zone, so when I request value.toLocaleDateString (), I will get the correct date. I can use this parser to get the date from any server. Moment library is very useful for parsing dates in any format, if you do not provide an input format, the moment can determine which format, but prefers to transmit the input format.

0
Jun 02 '18 at 8:25
source share

(new Date().toString()).replace(/\w+-\d+ \(.*\)$/,"")

This will be the way out: Tue July 10, 2018 7:07:11 PM

(new Date("2005-07-08T11:22:33+0000").toString()).replace(/\w+-\d+ \(.*\)$/,"")

It will be a way out: Fri Jul 08 2005 04:22:33

Note. The time returned will depend on your local time zone.

-2
Jul 11 '18 at 2:07
source share

There are some inherent problems with date syntax, which, unfortunately, are not fixed by default.

- Human readable dates have a hidden time zone in them
- There are many widely used date formats on the Internet that are ambiguous

To solve these problems easily and simply, you will need this function:

 >parse(whateverDateTimeString,expectedDatePattern,timezone) "unix time in milliseconds" 

I was looking for this, but found nothing!

So I created: https://github.com/zsoltszabo/timestamp-grabber

Enjoy it!

-7
Feb 20 '14 at 0:01
source share



All Articles