There is this web application that I’ve been working on.
I wanted to be able to handle globalization while displaying datetime on the web page.
This datetime was stored on the server in UTC form.
I wanted it to be displayed on the client-side i.e. the browser in the local time.
This was an ASP.Net MVC application and I initially added the following code snippet in the Razor view to display the datetime(@Model.PostedOn contained the UTC datetime)…
The next step was to convert this to the local time on the browser client.
While this could be one way to achieve it, I found that the third party library moment.js provided a much more cleaner way to approach this.
All I had to do was to add reference to the moment.min.js library and the above code could now be refactored as…
It helps achieve this in a single line of code and additionally also provides various parameters to handle the locale formats.
Let me know which approach you find convenient and the libraries you usually use for this purpose.