5

I am calling a .net asmx webservice that returns a number of fields. One of the fields in a date. The date is in the format of: "effective_date":"\/Date(978411600000)\/"

According to this SO question: How do I format a Microsoft JSON date? it would be better if the date returned was in ISO 8601 format, this way JavaScript would be able to interpret it as a date.

Currently I use the following javascript: new Date(d.effective_date) and I get the message Invalid Date. According to the linked SO question I should be able to do this if I can get the web service to pass the date in ISO format rather than in \/Date(978411600000)\/ format.

My question is, how do I get the webservice to return the date in ISO 8601 format?

Note: I'm aware that I can use this (per the answer from the linked question): var date = new Date(parseInt(d.effective_date.substr(6)));, however it is mentioned in a comment that Incoming date values should be formatted in ISO-8601, so I'm wondering how to get the incoming date from the web service to be in this ISO format.

Community
  • 1
  • 1
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • How is your web service? what are you using, asp.net, mvc, etc.? If you used `Json.Net`, you could use `DateFormatHandling.IsoDateFormat` – L.B Jan 01 '15 at 17:54
  • I'm using asp.net; I updated the tags to reflect. – kralco626 Jan 01 '15 at 18:16
  • @kralco626 did you find the solution? I am also experiencing the same issue and would like asmx web service to return the date in ISO-8601 format! I know I can add javascript code to make date compatible but would prefer that web service returns all dates in ISO-8601 format. – learner Dec 23 '15 at 14:35
  • p.s. I am using .net 4. – learner Dec 23 '15 at 14:40

1 Answers1

-1

You may use:

var date = new Date(d.effective_date);
date.toISOString(); // ISO-8601 formatted string

JSFiddle: http://jsfiddle.net/nanndoj/gjtkvrsy/

nanndoj
  • 6,580
  • 7
  • 30
  • 42
  • that is on the JavaScript side correct? I want to pass the date from the web service to javascript in ISO format. Right now, if I say new Date(d.effective_date) javascript gives me an Invalid Date message because the date sent from the web service is not formatted correctly. – kralco626 Jan 01 '15 at 16:28
  • I guess you are getting invalid date because your server is returning the data prefixed by a "Date(" string. You may remove this or eval("new " + d.effective_date) instead of put it as a new Date() argument – nanndoj Jan 01 '15 at 16:33
  • ya, I could do a substring on that string to remove the date and then parse the int as shown in the answer on the question I linked to. However, there is a way to get the web service to return the string in ISO format, which is preferred. I'm trying to figure out how to do this. – kralco626 Jan 01 '15 at 16:37
  • 1
    How does this post answer this: *"so I'm wondering how to get the incoming date from the web service to be in this ISO format"* ? – L.B Jan 01 '15 at 18:21
  • In all fairness I added the note in after this post, but it doesn't answer the question: `My question is, how do I get the webservice to return the date in ISO 8601 format?` which was there originally – kralco626 Jan 01 '15 at 19:24