Globalizing date & time format in MVC

697 views Asked by At

we are following MVC pattern, which is best place for converting toUTC and toLocal in controller or in javascript? Also whether we need to save the date format in invariant culture to support globalization? (or) is there any standard thumb rule/best practice for globalizing date time formats in MVC?

1

There are 1 answers

2
Matt Johnson-Pint On BEST ANSWER

There are benefits and drawbacks of each approach.

Server Side

  • You'll need to know the user's time zone. Usually this is done via user selection on some settings page within your app. Once you know the time zone, conversions usually done with TimeZoneInfo or with Noda Time, in your MVC controller.

  • You'll need to know the user's culture (ex. en-US or fr-FR, etc.) to display the correct format (ex. MM/dd/yyyy vs dd/MM/yyyy). This can be done via .NET globalization. The formatting should be done in your MVC view, not the controller.

  • You'll need to stay on top of time zone updates, as time zones rules can change as the governments of the world change their minds about their offsets and DST transition dates.

Client Side

  • You'll send just the UTC time to the browser, and let it convert to whatever the local time zone is. No need for the user to choose - as long as you only care about their current local time zone.

  • Getting the formatting right can still be tricky. Libraries like moment.js can help.

  • There are some bugs that may or may not affect your conversions. Be aware of them.


With regard to your question about the invariant culture - you would need to elaborate on what you mean by "save the date". If you mean storing to a database, such as SQL Server, the culture and format are irrelevant. Send a DateTime to your database, not a string.