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.
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.
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.
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
toLocaleDateStringinstead of hand-written solution bound to a specific locale (UK?)