5

I am pushing Values into a ko.observalbeArray with an AJAX call, I want to format the JSON return date to "YYYY-MM-DD" before I am pushing it into my observableArray.

The Specific element in my Code that I want to convert is: OrderTimeStamp: element.OrderTimeStamp Here is an example of a date that gets returned from server:

/Date(1377200468203+0200)/

Here is my AJAX call:

        $.ajax({
        url: "/[URL TO API Method]/GetAllOrdersbyparm",
        data: {Parm: ko.toJS(MyDataViewModel.SelectedParmater), Start: ko.toJS(MyDataViewModel.ParmStart), End: ko.toJS(MyDataViewModel.ParmEnd)},
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "JSON",
        timeout: 10000,
        success: function (Result) {
            for (var i = 0; i < Result.d.length; i++) {
                element = Result.d[i];
                MyDataViewModel.OrderDetails.push({ OrderID: element.OrderID, OrderGUID: element.OrderGUID, OrderTimeStamp: element.OrderTimeStamp, OrderStatus: element.OrderStatus, QtyProductsOnOrder: element.QtyProductOnOrder, PaymentDate: element.PaymentDate });
            }
        },
        error: function (xhr, status) {
            alert(status + " - " + xhr.responseText);
        }
    });
Jacques Bronkhorst
  • 1,685
  • 6
  • 34
  • 64
  • Is it your case? http://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format – Rango Aug 24 '13 at 18:42

2 Answers2

7

So, this is an ASP.NET specific Microsoft Date "standard".

See http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx why it should be avoided like the plague(1).

In that format the first component is a UTC milliseconds offset since the UNIX epoch. The offset is TO local time, which is the opposite of the timezone offset in the JS Date string representations.

Use

var dateString = myDate.toJSON();

to serialize a JS Date object for sending.

Such a serialized datetime string, which is also in UTC (aka *Z*ulu), can be used to create a Date object thusly:

var myDate = new Date(dateString);

(1) In case you need to support this old ASP.NET Date format you can convert it to a proper JS Date like this (thanks [Roy Tinker][2]):

myDate = new Date(parseInt("/Date(1377200468203+0200)/".substr(6)));

I'm not familiar with that particular datetime notation.

Is that home-grown?

Is there documentation for that?

If not, then you are setting yourself up for trouble trying to interpret it.

That conversion toJSON would make it a UTC time and a few things are open for interpretation, unless documented in minute (no pun intended) detail.

So this is my answer: Be very sure you have the above definition in normative writing.


Now let me ramble on for a bit:

I went through that little exercise here...

http://en.wikipedia.org/wiki/ISO_8601 would be a good standard to base datetime notation on.

Now, you already get that format from the server, which looks like a millisecond time value since the epoch ('1970-01-01T00:00:00Z'), (probably with a timezone offset already applied to it!) combined with a timezone offset string in HHMM.

That's a bit scary, since those two components don't mix well.

Evaluated as an expression 1377200468203+0200 would subtract octal! 200 milliseconds! from 1377200468203. That's clearly not what's intended.

In ISO8601 (which this notation is not) this timezone offset would be FROM UTC, so the millisecond value would already have the 2 hour, 0 minutes offset applied to it.

Now the code could of course run on a machine which is in a different timezone than the datetime given.

The very crucial question is whether this millisecond datetime value has indeed the offset FROM UTC in it.

In that case, doing var dt = new Date(1377200468203); would be wrong.

If it is close to a daylight savings time switch time, it would be incorrect to just subtract to offset from it.

phloopy
  • 5,563
  • 4
  • 26
  • 37
stackunderflow
  • 953
  • 7
  • 17
  • Food for thought, we are currently on the ISO 8601 format, but not to sure about app deployment time zones. Here http://en.wikipedia.org/wiki/Date_and_time_notation_in_South_Africa here is what I am referencing. The important part for me is at the end of the day, where ever the application set would be deployed, would the end user be able to obtain orders between 2 dates – Jacques Bronkhorst Aug 24 '13 at 18:40
  • 1
    I guess a solid design would keep all datetimes in UTC and the timezone of the creator would just be recorded for proper presentation. Or you could guess if user in +0530 (Chennai) might be awake right now to talk to user in -0800 (Bakersfield) for the sake of an example. – stackunderflow Aug 24 '13 at 18:52
  • All great, but how do I add the date formatted date to my Ajax Call :) here is my Fiddle to show what I am getting as well as Full AJAX call:http://jsfiddle.net/Fumunchu/rhW2y/ – Jacques Bronkhorst Aug 24 '13 at 19:03
  • In "would the end user be able to obtain orders between 2 dates" is that order as in sorting or buying? – stackunderflow Aug 25 '13 at 12:10
1

Note, not sure if below answers your question. If not, you may be helped by this one: How to format a JSON date?

Something along these lines should work:

var yyyy = element.OrderTimeStamp.getFullYear()
var mm = element.OrderTimeStamp.getMonth();
var dd = element.OrderTimeStamp.getDate();

var x = yyyy + '-' + (mm < 10 ? '0'+mm : mm) + '-' + (dd < 10 ? '0'+dd : dd)

element.OrderTimeStamp = x;

See this fiddle for an example. For reference, the MDN page has good documenation on Dates.

If you need more advanced date and time functionality I can recommend looking at MomentJS.

Community
  • 1
  • 1
Jeroen
  • 60,696
  • 40
  • 206
  • 339
  • Thanks for the feedback, I am not to sure how I am suppose to include the above mentioned function into my AJAX call – Jacques Bronkhorst Aug 24 '13 at 17:52
  • Hmmm? Could you include in the question or a fiddle what you've tried, and include why it didn't work? Between my code and the linked question it looks straightforward to me. – Jeroen Aug 24 '13 at 18:08
  • Here is the fiddel with the result I am getting: http://jsfiddle.net/Fumunchu/rhW2y/ – Jacques Bronkhorst Aug 24 '13 at 18:27
  • 1
    Jacques, your jsFiddle does not specify any libraries. You are using jQuery, right? Which version? I get Uncaught ReferenceError: $ is not defined for this http://jsfiddle.net/Fumunchu/rhW2y – stackunderflow Aug 24 '13 at 22:51