Presentation of dates sent over JSON in Single Page Applications
I'm developing a single page application using backbone.js, mustache and jquery for the front end and Groovy on Grails for the backend. My app needs to show some dates to the user in the interface (exact dates formatted according to the user's locale rather than "30 days ago" or whatev开发者_运维问答er), and all data is sent to the UI using JSON with the dates encoded in ISO 8601 standard.
However when I need to show a date on screen i'm not sure of the best way of going about this, should I use javascript to convert the date to something human readable? And how do I get the user's locale and work out what the date format is? Is there a good library to do this? Another approach would be to include the date in the JSON twice, once using ISO8601 and another human-readable given the locale in the request header.
Just wondered if other people have tackled this and what your final solutions were. Cheers.
If you really need to send ISO8601 (like you use this representation somewhere), you probably need to create your own, custom solution (no cross-browser formatting method in native JavaScript exists).
There is a JQuery Globalization plugin which will allow you to format dates correctly but I didn't have time to look into it, so I am not sure what is its input.
On the other hand, if you don't have to do anything to dates maybe sending out already formatted dates would be a good idea (it is certainly easier to implement)...
精彩评论