Get Client-Size TimeZone offset to Serverside to get local times of dates stored as utc for MVC app


I am trying to display a table showing meeting dates that I have saved as UTC in the local time.  I think I know how to get the local timezone offset ??
 var d = new Date()
            var javaScriptTimeZoneOffset = d.getTimezoneOffset();

Open in new window

However I can't figure out how to use that server-side to generate the ViewModel that I need to display the time-zone adjusted dates.  My code looks like this:

public ActionResult AvailableMeetings()
            //var utcOffset = ?? - get from javaScriptTimeZoneOffset 

            var meetings = _context.Meetings
                .Select(meeting => new TimeZoneAdjustedViewModel
                    Id = meeting.Id,
                    Start = DateTime.SpecifyKind(meeting.Start, DateTimeKind.Utc).AddMinutes(utcOffset),

            return View(meetings);

Open in new window

Any help in accomplishing this would be greatly appreciated.

Who is Participating?
greetings RBS, , there are essentially two ways that I know to do this, , You can use the getTimezoneOffset( ) to get the Minutes from GMT to JS local time-
    var d = new Date()
    var offMinutes = d.getTimezoneOffset();

and then change the minutes (milliseconds) in the server date.

OR, it may be possible to use -
    var dt = new Date(Date.UTC(2015,06,22,10,45));

depending on how you get the server UTC (GMT) JS time variable.
I would suggest that you keep the server blissfully unaware of client's timezones. Keep the dates in UTC and for any conversions/input/presentation use a javascript date library like
RBSAuthor Commented:
Thank you - but that does not answer my question.  I need to get the offset to store in a session variable or whatever so that I can present the available times in the user's local time zone.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Bob LearnedCommented:
If you have a UTC time, then you should be able to use the DateTime.ToLocalTime method to convert to local time, depending on the current locale.
RBSAuthor Commented:
Hi Bob:

Thank you.  DateTime.ToLocal Time returns the utc offset based on where the server is located.  I need to get the utcOffset based on where the client machine is - i.e. the time zone set in the user's computer clock - that's why I'm using javascript.

Here's one potential approach:
var tzoMinutes = - (new Date).getTimezoneOffset();
document.cookie = "tzoMin=" + tzoMinutes;

Open in new window

The first line is basically what Slick812 suggested.

The second line stashes the result in a browser cookie.  On successive page-requests made by that browser, the server can grab that value from the request's cookies.  For example:
string tzoMin = Request.Cookies["tzoMin"]; // or equivalent, depending on exactly where this code is (page, controller, view, etc.)
int utcOffset = 0;
if(!string.isNullOrEmpty(tzoMin)) {
    int.TryParse(tzoMin, out utcOffset);

Open in new window

Hope that helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.