Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format JSON date before pushing into ko.observableArray

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);
        }
    });
like image 667
Jacques Bronkhorst Avatar asked Aug 24 '13 17:08

Jacques Bronkhorst


2 Answers

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.

like image 178
stackunderflow Avatar answered Oct 20 '22 00:10

stackunderflow


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.

like image 24
Jeroen Avatar answered Oct 20 '22 00:10

Jeroen