NotionCommotion Posted October 30, 2014 Share Posted October 30, 2014 I asked a similar question on http://forums.phpfreaks.com/topic/291982-where-should-dates-be-formatted/, but never received an answer to the question I really needed help with. Server can provide some JSON such as the following. I wish the date to be displayed like "10/09/2014 09:31:41 AM". I "could" use the server to format the date, but I understand that this is bad practice as it is not the view. Note that I might be using handlebars to render the JSON to HTML, however, I don't think this is relevant. Please provide recommendations on where and how the date should be formatted. Thank you [ {"id":123,"filename":"someFile1.pptx","datetime":"2014-10-09 09:31:41","size":"6299 KB"}, {"id":321,"filename":"someFile2.pptx","datetime":"2014-10-29 04:35:42","size":"4629 KB"}, {"id":444,"filename":"someFile3.pptx","datetime":"2014-10-19 02:33:43","size":"6599 KB"} ] Quote Link to comment https://forums.phpfreaks.com/topic/292155-where-should-dates-be-formatted-when-provided-to-client-using-json/ Share on other sites More sharing options...
QuickOldCar Posted October 30, 2014 Share Posted October 30, 2014 Use timestamp. 2014-10-09 09:31:41 what timesone is that? am or pm? I've seen it both ways, do both if you want. If another server wanted to store the data their database a timestamp is better. If they want to grab your data and display it, timestamp still works with minimal effort. If they get a formatted date, they have to parse it or run through strtotime and back to date. Consider american, european and iso formatting differences American M/D/Y European D-M-Y ISO Y.M.D. I've seen uk sites format their dates on output to slashes and their orders. Timestamp is always right because is utc, set the locale, what parts want to display as a date and are done. When it comes down to it...timestamp is the best choice. Quote Link to comment https://forums.phpfreaks.com/topic/292155-where-should-dates-be-formatted-when-provided-to-client-using-json/#findComment-1495213 Share on other sites More sharing options...
kicken Posted October 30, 2014 Share Posted October 30, 2014 You could format the date client-side using either the built in Date object, or a library like Moment.js. If you don't mind adding another library to your page, I'd take the library route and format it client side. Otherwise I would probably just formatted it server-side and return it either along with or instead of the current date format you have, depending on your needs. Quote Link to comment https://forums.phpfreaks.com/topic/292155-where-should-dates-be-formatted-when-provided-to-client-using-json/#findComment-1495214 Share on other sites More sharing options...
NotionCommotion Posted October 30, 2014 Author Share Posted October 30, 2014 Thanks Kicken, I've never used Moment.js, but was considering it. Or as you suggest, maybe I should just format it server-side and be done with it. Quote Link to comment https://forums.phpfreaks.com/topic/292155-where-should-dates-be-formatted-when-provided-to-client-using-json/#findComment-1495250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.