I've been looking for an answer since yesterday with no avail. I've tried things like Model binders, layout templates (doesn't apply on my case), etc.
Let me clarify what I actually need.
I have many DateTime fields on many tables in a SQL Server 2008 R2 database. They're stored on the London Timezone. I've managed (with help from you guys) to convert the DateTime to the user's Timezone and display them correctly. What I need now is a way to automate this. Everytime the site displays DateTime coming from the database, it needs to be converted beforehand. The input will stay as it is.
If it was on WebForms development - that I'm used to - I'd just create a handler. But I don't know what's the best practice on MVC 3 Razor.
Does anyone can give me some directions here, please?
Thanks in advance.
Complementing
That's the code that converts the DateTime:
// Timezone data provider
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
// Database Timezone = London
var londonTimeZone = timeZoneProvider["Europe/London"];
//// Getting date/time from the database and mapping it as being local to London timezone
//var yourDateFromDb = new DateTime(source.Year, source.Month, source.Day, source.Hour, source.Minute, source.Second, source.Millisecond);
//ZoneLocalMapping map = londonTimeZone.MapLocal(LocalDateTime.FromDateTime(yourDateFromDb));
//// This is the date/time with the correct offset (taking into account DST etc)
//var zonedDbDateTime = map.First();
// Jon Skeet suggestion instead of using the Mapping
// http://stackoverflow.com/questions/16164994/datetime-conversions-using-nodatime-on-asp-net-mvc-3-razor-website-how-to/16178730?noredirect=1#16178730
//
var dateTimeFromDb = new DateTime(source.Year, source.Month, source.Day, source.Hour, source.Minute, source.Second, source.Millisecond);
var zonedDbDateTime = londonTimeZone.AtLeniently(LocalDateTime.FromDateTime(dateTimeFromDb));
// Map the London zoned date/time to the users local date/time (saved on the Session)
var usersTimezoneId = (HttpContext.Current.Session["timezoneid"] != null ? HttpContext.Current.Session["timezoneid"].ToString() : "UTC");
var usersTimezone = timeZoneProvider[usersTimezoneId];
var usersZonedDateTime = zonedDbDateTime.WithZone(usersTimezone);
return usersZonedDateTime.ToDateTimeUnspecified();