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

Hi:

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

            return View(meetings);
        }

Open in new window


Any help in accomplishing this would be greatly appreciated.

RBS
RBSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ambienceCommented:
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 https://code.google.com/p/datejs/wiki/APIDocumentation
0
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.
0
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.

RBS
0
Slick812Commented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThogekCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.