30-seconds-of-code: JSON to Date

Created on 16 Dec 2017  路  14Comments  路  Source: 30-seconds/30-seconds-of-code

I have no idea where did you get that format "/Date(_timestamp_)/", but
new Date().toJSON() returns an ISO representation like 2017-12-16T18:16:36.514Z

Also instead of manually formatting a date, you can use toLocaleDateString, which is supported literally in every browser in its basic form.

question

Most helpful comment

I think that this snippet a good example of how to parse a custom Date serialization.
I will create a PR with toLocaleDateString instead of hand-written solution bound to a specific locale (UK?)

All 14 comments

Uhm, I can't remember who PRed that, maybe they know where that came from. 馃槄

@Chalarangelo I PRed fixed function that someone else made. Cannot remember who did it but it was in the last 24hours I think.

152 I found it. I just made it more ES6 friendly but didn't check much on the functionality.

The dev stated that the passed date comes from ajax as JSON.

I didn't check that but I don't think I have seen that in my life before. @sekmenhuseyin could you explain a bit further where such data would come from?

I am not sure but he mentioned it comes from ajax req.

For example:
I have a table with a date column in it. I send this table from controller to view with jsonresult. thanks to json serializer, these date column datas change. To read this data we need to use a javascript format function.

I have honestly never seen this before, but if you say so, I think we should keep the current implementation. Anybody objecting to that decision?

That's an old (but obviously still in use) Microsoft AJAX response. There is no Date literal in JavaScript (and hence JSON) so the onus is on the developer to verify that the response is correctly formed - as it is with newer implementations - and either transform the value before encoding, if possible, or afterward with a client-side function.

This does mean that you have to know what the data type of a particular point is supposed to be...

I think that this snippet a good example of how to parse a custom Date serialization.
I will create a PR with toLocaleDateString instead of hand-written solution bound to a specific locale (UK?)

@kirilloid see https://weblog.west-wind.com/posts/2014/jan/06/javascript-json-date-parsing-and-real-dates

There's a function that deals with Microsoft date-constructors-as-strings-in-JSON in that page.

@ShirtlessKirk I think some points in this article are obsolete. E.g. parsing ISO time representation is supported OOTB with native Date.parse and is a trivial thing.
But the whole goal of this project as I understand it is about to show how to do some non-trivial things. W/o even thinking to certain format mentioned here, the snippet is a good demonstration on how to handle that..

Alright, I think the snippet's usefulness has been justified and the question answered, so I'm closing the issue. If anyone wants to submit a PR with improvements or anything like that, feel free to do so!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skatcat31 picture skatcat31  路  5Comments

Speuta picture Speuta  路  3Comments

YusofBandar picture YusofBandar  路  5Comments

larrybotha picture larrybotha  路  3Comments

Lucien-X picture Lucien-X  路  5Comments