Using Dates in your Front End application

Nasreddine Skandrani
Front-End Tricks — TheBlog
2 min readDec 14, 2020

Short article to discuss the topic of Dates usage in Front End applications.

What you should be probably doing in your Front End?

Option 1

Using the format https://en.wikipedia.org/wiki/ISO_8601 to receive all dates from the Back End and send them back to it. Use UTC dates of course formatted in ISO.

If you want to learn more about why UTC and what to do if the Front End is not under your control, read here.

Like the database, to keep your API consistent, HTTP responses payloads should always be in UTC. However, your API should follow the robustness principle: Be conservative in what you do, be liberal in what you accept from others. Thus, your API should parse any timezone in the HTTP request body or URL parameters and not require UTC. Some APIs will default the absence of timezone information as UTC.

Option 2

Using timestamps to receive all dates from the Back End and send them back to it.

~ ~ ~ ~ ~ ~ ~ ~ ~

Note 1:
It’s more difficult to debug timestamp than ISO in the network tab of chrome :)

Note 2:
You don’t need to have a type Date received from the Back End in your DTO types. They can remain in string with the right format if you don’t do something special with them.
In my case personally, it’s Date that is used in the DTO. I find it more easy if i have to format to locale time later.

Thanks to my friend Amin Paks for the support in the topic.

@FrontEndTricks

--

--