NotionCommotion Posted October 22, 2014 Share Posted October 22, 2014 When just generating HTML, where should dates be formatted? What about when generating JSON (I've found that date formatting is not so straight forward using JavaScript)? Are there factors which may make one approach better than the other (i.e. querying one record versus a list of records)? How important is consistency of an approach? Any other factors I should consider? Please provide rational for your decision. Thank you At the database? DATE_FORMAT(my_date, "%m/%d/%Y %r") AS my_date In the controller? $date = new DateTime($my_date); $my_date=$date->format('m/d/Y'); In a twig or smarty template? {{ mydate|date("m/d/Y g:i A") }} At the client using handlebars and the Moment library? UI.registerHelper("formatDate", function(datetime, format) { if (moment) { f = DateFormats[format]; return moment(datetime).format(f); } else { return datetime; } }); ... {{formatDate MyISOString "short"}} Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 22, 2014 Share Posted October 22, 2014 Formatting is a presentational task, so it should be done in the template when the output is generated. It's a bad idea to format the date at database level or in the controller: What if you need to actually do something with the date in the application? Then the custom format will be in the way. What if you decide to support multiple formats? Do you put a big CASE block into every single query? Do you write a custom SQL function only to format dates? Both will be a pain in the ass. You might also have to pass a format parameter to the database system so that it knows which format to choose. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 22, 2014 Author Share Posted October 22, 2014 Thanks Jacques, What if the server is generating JSON instead of HTML? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 22, 2014 Share Posted October 22, 2014 What is the JSON document used for? In general, JSON documents carry raw data, so you'd use the raw, unformatted date. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 22, 2014 Author Share Posted October 22, 2014 What is the JSON document used for? In general, JSON documents carry raw data, so you'd use the raw, unformatted date. The JSON document is a list of data which is used to add content to the current page where the dates should be formatted like "10/09/2014 09:31:41 AM". Yes, it "could" be parsed client side, however, JavaScript doesn't seem to have a clean way of doing so. [ {"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 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.