12

What's the easiest way to get the minutes if I only have a string that represents the timezone? (for example "Europe/London")

So far I have:

public static int ConvertFromTimeZoneToMinutesOffset(string timeZone)
{
    DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZone];

    // Need magic code to get offset as minutes
}

But I'm not finding an easy approach to convert it to minutes.

NOTE: What I need is the offset in that specific moment in which the code is being executed. Between that timezone and UTC.

Axel Prieto
  • 535
  • 10
  • 20

2 Answers2

25

You need DateTimeZone.GetUtcOffset(Instant):

public static int ConvertFromTimeZoneToMinutesOffset(string timeZone, IClock clock)
{
    DateTimeZone zone = DateTimeZoneProviders.Tzdb[timeZone];
    Offset offset = zone.GetUtcOffset(clock.Now);
    return offset.Milliseconds / NodaConstants.MillisecondsPerMinute;
}

You could leave off the IClock parameter and instead use SystemClock.Instance in the method, but that leads to code which is harder to test.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
-2

Use the DatePart() function on minutes.

Jim Snyder
  • 99
  • 2
  • 9
  • That's not enough. I don't have a "DateTime", I have a TimeZone. And I need the minutes, so instead of "America/Buenos Aires" I need -90 (the offset in minutes) – Axel Prieto Oct 12 '16 at 19:25