Stanley
Stanley

Reputation: 1441

TimeZone.CurrentTimeZone alternative

I'm using TimeZone.CurrentTimeZone to get user's time offset from UTC like so:

TimeZone zone = TimeZone.CurrentTimeZone;
TimeSpan offset = zone.GetUtcOffset(DateTime.Now);
return offset.Hours*60+offset.Minutes;

This works when I build for Android,iOS,Blackberry, but on WM8 I get the following build error:

Error: the 'System.TimeZone' doesn't exist in the target framework. I know the target framework is a subset of ASP.NET 2.0

Can anyone suggest an alternative way of getting UTC offset?

Upvotes: 6

Views: 6277

Answers (2)

Roberto
Roberto

Reputation: 11933

TimeZoneInfo and its GetUtcOffset(DateTime) are supported in .NET for Windows Store Apps.

So you may just do:

TimeSpan delta = TimeZoneInfo.Local.GetUtcOffset();
double offset = delta.TotalMinutes;

Upvotes: 4

Stanley
Stanley

Reputation: 1441

found the answer here:

UTC offset in minutes

not the accepted answer but this:

 (DateTime.UtcNow - DateTime.Now).TotalMinutes;

Upvotes: 0

Related Questions