Jump to content

Where should dates be formatted when provided to client using JSON?


NotionCommotion

Recommended Posts

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

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.

Link to comment
Share on other sites

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.

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.