How to get timezone?

4

I have the following code:

var minhaData = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, meuTimeZone);

If meuTimeZone = "E. South America Standard Time"; then the value returned to minhaData is timezone -03:00 .

Is it possible to somehow get this value in hours (in this case -03:00 ) based on meuTimeZone or minhaData ?

    
asked by anonymous 22.09.2015 / 21:32

1 answer

3

I would do this:

using System;
using static System.Console;

public class Program {
    public static void Main() {
        var minhaData = DateTime.UtcNow;
        WriteLine($"Hora universal {minhaData.ToLocalTime()}");
        var fuso = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time");
        var diferenca = new DateTimeOffset(minhaData, TimeSpan.Zero);
        WriteLine($"Hora local {diferenca.ToOffset(fuso.GetUtcOffset(diferenca))}");
        WriteLine($"Diferença {fuso.GetUtcOffset(diferenca)}");
    }
}

See working on dotNetFiddle .

    
22.09.2015 / 21:52