Parse date without timezone javascript

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, not String .


  • 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.


    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);
    

    I ran into the same problem and then remembered something wonky about a legacy project I was working on and how they handled this issue. I didn't understand it at the time and didn't really 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 whatever reason passing the date in as '01-02-2014' sets the timezone to zero and ignores the user's timezone. This may be a fluke in the Date class but it existed some time ago and exists today. And it seems to work cross-browser. Try it for yourself.

    This code is implemented in a global project where timezones matter a lot but the person looking at the date did not care about the exact moment it was introduced.

    链接地址: http://www.djcxy.com/p/3074.html

    上一篇: 使用类似的参数调用Date构造函数时会提供意外的结果

    下一篇: 解析日期没有时区的JavaScript