Parse date without timezone javascript

2019-01-05 01:34发布

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:

  1. Without time zone.

  2. Without calling constructor Date.UTC or new Date(year, month, day).

  3. Just simple passing string into Date constructor (without prototype approaches).

  4. I have to product Date object, not String.

9条回答
Luminary・发光体
2楼-- · 2019-01-05 01:40

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 of Date object by creating your own one. This is, however, better to be handed over to libraries like moment.js.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-05 01:40

(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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-05 01:49

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:

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

getTimezoneOffset() will return ether negative or positive value. This must be subtracted to work in every location in world.

查看更多
祖国的老花朵
5楼-- · 2019-01-05 01:49

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 and minutes values from the previously created moment object:

  const hours = dateMoment.hours();
  const mins = dateMoment.minutes();

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.):

  const dateObj = dateMoment.toDate();

Step 4. Manually set the hours and minutes extracted in Step 2.

  dateObj.setHours(hours);
  dateObj.setMinutes(mins);

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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-05 01:53

The date is parsed correctly, it's just toString that converts it to your local timezone:

> 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"

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.

查看更多
Ridiculous、
7楼-- · 2019-01-05 01:55

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:

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

I have searched for this, but found nothing like that!

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

Enjoy!

查看更多
登录 后发表回答