I'm currently considering approaches for implementing DateTime localization, taking into account a users timezone and user culture to decide the local time and date format for our MVC4 application.
The aims (if possible):
- Convert a users local DateTime to UTC dates for use on Controllers and the Database
- In all ViewModels/JsonResults, convert UTC to a users local DateTime and format for their culture in for use in views and for use in JSON feeds for third party controls
Users in our system all have accounts, and they have specifically set their Timezone and Culture settings, so I will always have access to those elements.
Local -> UTC (Solved)
For 1. I have a Custom Model Binder that takes the users local time and converts the time to UTC, which works as expected.
UTC -> Local
For 2. I'm not sure what the best way to tackle this. I really want to intercept all of the DateTime properties, and format the value from UTC to a users local time/culture before they're returned to the View or returned as a JSON object.
Can is there a generic way I do this using something like an ActionFilter? It would be great if I could find a way to do this in one place that would format DateTimes for use in both my Views and JSON results.
I know I could split this out, and do this in Display/Editor templates, but the problem I then have is then the UTC dates being returned in the JSON feed for Third party controls.