简体   繁体   中英

Javascript datetime strings ignoring timezone offset (experiment)

My worst nightmare are date objects, so I've created a fiddle to see how it works, and try to find a solution for date strings in the following format 2015-10-05T11:49:13.587Z but taking care of current timezone.

Basically, I want to be able to write new Date() (or similar) and get 2015-10-05T09:49:13.587Z instead of 2015-10-05T11:49:13.587Z .

It seems that the only methods displaying the correct date/time are:

  • moment().format()
  • new Date().toLocaleString()
  • moment().toLocaleString()
  • moment().format().toLocaleString()

But none of them are returning a date object like the one I want. What's the proper method to deal with it then?

Please, check my tests: http://codepen.io/Frondor/pen/PPmYpv

Basically, I want to be able to write new Date() (or similar) and get 2015-10-05T09:49:13.587Z instead of 2015-10-05T11:49:13.587Z .

The value you're asking for is two hours earlier. It would be invalid to convert it in this way.

I think you are confusing two very different things:

  • UTC is not a format. It is a system of timekeeping. When we say "in UTC", we mean that the time value is presented in Universal Time - which is the same all over the planet, and is roughly the same as GMT (the time in Greenwich, England without considering daylight saving time). If you're local time is two hours behind UTC, then UTC is two hours ahead of your local time.

  • The ISO-8601 format is a string representation for dates and times. There are several forms, and the one you've expressed is the extended format of a timestamp . In other words, it represents the actual instant that an event occurs, in terms of a date, a time, and a relationship to UTC.

    In this format, the Z specifically means "the value presented here is already in terms of UTC". It is roughly the same as using +00:00 .

Therefore, if the value in UTC as an ISO8601 string is 2015-10-05T11:49:13.587Z , then the corresponding local value (assuming UTC-2 as you stated), is 2015-10-05T09:49:13.587-02:00 . This is still in ISO8601 format, but is no longer in UTC.

The easiest way to get this is with moment.js, using moment().format() .

This format - 2015-10-05T11:49:13.587Z - is one of the standard representation of date and time in Coordinated Universal Time (UTC) or the ISO 8601 format.

The timezone will not be shown in this format because the purpose of the format itself is (as explained in the Wikipedia article):

Representation of dates and times is an international standard covering the exchange of date and time-related data.

But we can definitely show the date in that format along with the offset for the current timezone, but it defeats the purpose of that "standard" format itself.

You're asking for it to produce an inaccurate time. The format you're looking for implies a time in UTC. I would suggest you review your requirement, as there may be a flaw there.
But if that's exactly what you want, you would say:
moment().format("YYYY-MM-DDThh:mm:ss.SSSS") + 'Z'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM