0

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();
emerson.marini
  • 9,331
  • 2
  • 29
  • 46
  • One option you may want to consider would be to write a custom HTML helper (like Html.TextBoxFor..) which would be strongly typed to DateTime. You can stash all your logic neatly inside the custom helper and use it whenever you want to render the date time property – Chintana Meegamarachchi Apr 25 '13 at 01:51
  • @ChintanaMeegamarachchi, thanks for that. I ended up going for an extension instead. – emerson.marini Apr 25 '13 at 08:02

1 Answers1

0

I've found a solution going for a DateTime extension:

public static DateTime ToLocalTimeZone(this DateTime dateTime) { 
    IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb; 
    var londonTimeZone = timeZoneProvider["Europe/London"];

    var dateTimeFromDb = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,
       dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond); 
    var zonedDbDateTime = londonTimeZone.AtLeniently(LocalDateTime.FromDateTime(dateTimeFromDb));

    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(); 
}

Not sure about best practices, because as I've pointed out, I'm totally new to the MVC model. But it's doing the trick.

emerson.marini
  • 9,331
  • 2
  • 29
  • 46