Jump to content

Where should dates be formatted?


NotionCommotion

Recommended Posts

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"}}
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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"}
]

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.