How to get Timezone?

Asked

Viewed 156 times

4

I have the following code:

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

Case meuTimeZone = "E. South America Standard Time"; then the value returned to minhaData stays with Timezone -03:00.

It is possible somehow to obtain this value in hours (in this case -03:00) on the basis of meuTimeZone or in minhaData?

  • I don’t understand the point, explain it better.

  • Type minhaData.ToString("zzz");?

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)}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future referenceto.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.