user3125433
user3125433

Reputation: 1003

Date().getTimezoneOffset() in Javascript vs TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow).TotalMinutes in .net server side

I have a javascript function used in my application

Date().getTimezoneOffset();

I need server side function to replace the same. I am using

TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow).TotalMinutes;

Is there any difference between the two, other than -ve and +ve value? Can I use this server side method in place of this javascript function.

Upvotes: 0

Views: 358

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241573

They are the same except the sign (+/-), but keep in mind:

  • They both return the current local offset. The offset for some other point in time in the same time zone may be different. See "Time Zone != Offset" in the timezone tag wiki.

  • "Local" means local to where the code is executing. So in a web application, the server-side code is using the time zone setting of the server. It has no idea about the time zone of your user.

Upvotes: 0

Arun Mohan
Arun Mohan

Reputation: 101

According to the Mozilla Developer Docs for JavaScript states,

The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.

The method summary comments for GetUtcOffset in .NET describes

Summary:

Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

Essentially both do almost the same job, except the .Net returns a TimeSpan object whereas the JavaScript's is just a number (the offset difference in minutes)

The TotalMinutes property value in the TimeSpan object infact returns the same value as JavaScripts'

Upvotes: 1

Related Questions