I have the Date/Time stored in the database on the London's time zone (not UTC). What I need is to retrieve this Date/Time, convert it to UTC and display considering the user's time zone (which they define when registering to the site - ie: en-GB, en-US, etc).
My first question is more related to the MVC structure, as I'm totally new to this (am a WebForms developer) - Where should I put this conversion code/class/helper? Model? ViewModel?
The second question is the conversion itself - I've played around with the NodaTime test project but can't find a proper way to do this conversion.
Any help is much appreciated.
Thank you in advance.
Jon Skeet may need to correct me, as I'm assuming this is correct in nodatime:
You should probably be aware that during timezone transitions (autumn clock change), you may get 2 possible dates for the zonedDbDateTime. The code here just gets the first or earliest date/time.
Edit: Updated code snippet with changes suggested by Jon Skeet
you can write some extension method like this :
and you will be able to call it like @Model.MyDate.ConvertToUtc() from any view you wish as long as you have the namespace of the ConvertToUtc inclucded on the top of page. and to do the conversion see this pages
Convert DateTime from UTC to a user provided time zone with C#
http://www.dotnetcurry.com/ShowArticle.aspx?ID=593
How to work with time zones in ASP.NET?