Ignore timezone offset when converting date - javascript

I have a WCF REST service that returns a date in the following format:

/Date(1401993000000+0530)/

      

The value of this date on the server

6/6/2014 12:00:00 AM

      

I want to parse this in my javascript code in UTC value.

I tried to manually remove the string "+0530" and parse it to date, but it gives "Invalid date".

I also tried adding the timezone offset according to this post, but it gives the wrong value.

How can I parse this?

+3


source to share


2 answers


This format is commonly referred to as "ASP.NET JSON Date" because it first came from the classes JavaScriptSerializer

and DataContractJsonSerializer

used in ASP.NET and other parts of .NET. However, it was heavily criticized and eventually deprecated in favor of the ISO 8601 standard , which is the default in the Json.Net library used in most modern .NET codes. You will still see it in WCF and older versions of ASP.NET MVC.

This format has two main options:

  • /Date(1401993000000)/

    - only timestamp
  • /Date(1401993000000+0530)/

    - time stamp with offset

Sometimes you will see the leading slashes being escaped with backslashes like in \/Date(1401993000000)\/

, depending on how it was generated. This should be allowed by parsers, but you shouldn't depend on it.

In both formats shown, the timestamp portion is intended to represent the number of milliseconds since Unix Epoch was released, which is 1970-01-01 00: 00: 00.000 UTC.

I say "intended" because in .NET it is possible to have a DateTime

s DateTimeKind.Unspecified

that cannot be mapped to UTC. In this case, the serializer will act as if it had DateTimeKind.Local

. The result will then display the value adjusted for UTC in the local time zone of the computer, as well as the UTC offset of the computer for that point in time. Ideally, you shouldn't rely on this behavior, as you will get different results from computers in different time zones.

When an offset is present in the output string, it is in the +HHmm

/ format -HHmm

, the positive values ​​of which fall east of GMT β€” in the same direction as the ISO 8601 standard. However, unlike ISO 8601, part of the value is not adjusted for this offset. It remains UTC-based.



In other words:

  • /Date(1401993000000)/

    = 2014-06-05T18:30:00Z

  • /Date(1401993000000+0530)/

    = 2014-06-05T18:30:00Z

    + +0530

    =2014-06-06T00:00:00+05:30

Because of this, some of the offset is extraneous when using this value to create a JavaScript object Date

- because the object Date

wraps the UTC timestamp and has no reserve to store the provided offset.

You can, of course, split the string into your own parts and use them yourself, but consider using Moment.js to parse that string instead . It understands the format natively and can return you an object that retains the offset knowledge.

var m = moment.parseZone("/Date(1401993000000+0530)/");

m.format() // "2014-06-06T00:00:00+05:30"

      

If you were looking for an object Date

, you can of course call m.toDate()

. The resulting object Date

will have the same UTC timestamp, but because of the way the object works Date

, any local time functions will only use the host environment offset.

In other words, with the object exiting, Date

some of +0530

your input becomes useless. You may have analyzed well /Date(1401993000000)/

.

+2


source


You can use Moment JS, one of the best way to play datetime in JavaSCript. Lots of functions

https://momentjs.com/timezone/

See edited post without instant messages:



 var data = [ {"id":1,"start":"/Date(1401993000000+0530)/"} ];
	 var myDate = new Date(data[0].start.match(/\d+/)[0] * 1);
	 myDate =  new Date(myDate.getTime() + myDate.getTimezoneOffset() * 60 * 1000);
	 alert(myDate);
      

Run codeHide result


0


source







All Articles