Current time of the states of Brazil C#

Asked

Viewed 4,261 times

1

I’m trying to get the current time of the states/DF Brazil with different time. I found the code below but it didn’t help much. Does anyone know any way to do it without webservice?

public static void Main()
{
    DateTime timeUtc = DateTime.UtcNow;
    var kstZone = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time");
    var horaBrasilia= TimeZoneInfo.ConvertTimeFromUtc(timeUtc, kstZone);
    Console.WriteLine(String.Format("Hora Brasilia: {0}",horaBrasilia));
    //Console.WriteLine(String.Format("Hora Amazonas: {0}",horaAmazonas));
    //Console.WriteLine(String.Format("Hora Acre: {0}",horaAcre));
    //Console.WriteLine(String.Format("Hora Piaui: {0}",horaPiaui));
}
  • 1

    Try to explain better what you want and what result you expect to get. try giving an example

  • almost that: http://www.apolo11.com/tictoc/fuso_horario.php

1 answer

5


The territorial extent of Brazil comprises 4 time zones (-2hs, -3hs, -4hs and -5hs in relation to Greenwich). You can get the time in each of these spindles through the following code:

        Console.WriteLine ("Exibir horários conforme TimeZone");
        DateTime timeUtc = DateTime.UtcNow;
        var noronha = TimeZoneInfo.FindSystemTimeZoneById("Brazil/DeNoronha");
        var brasilia = TimeZoneInfo.FindSystemTimeZoneById("Brazil/East");
        var amazonas = TimeZoneInfo.FindSystemTimeZoneById("Brazil/West");
        var acre = TimeZoneInfo.FindSystemTimeZoneById("Brazil/Acre");

        Console.WriteLine(String.Format("UTC Time: {0}",timeUtc.ToString()));
        Console.WriteLine(String.Format("Hora Noronha: {0}",TimeZoneInfo.ConvertTimeFromUtc(timeUtc, noronha).ToString()));
        Console.WriteLine(String.Format("Hora Brasilia: {0}",TimeZoneInfo.ConvertTimeFromUtc(timeUtc, brasilia).ToString()));
        Console.WriteLine(String.Format("Hora Amazonas: {0}",TimeZoneInfo.ConvertTimeFromUtc(timeUtc, amazonas).ToString()));
        Console.WriteLine(String.Format("Hora Acre: {0}",TimeZoneInfo.ConvertTimeFromUtc(timeUtc, acre).ToString()));

However, this does not correspond to the answer according to each region of the country. For example, the North region has part of its territory with -5hs (Acre), -4hs (Amazonas, Rondônia and Roraima) and -3hs (Pará, Amapá and Tocantins) in relation to Greenwich. The Midwest region, with the exception of Goiás (-3hs) is framed in the zone of -4hs. The rest of Brazil (Northeast, Southeast and South) has a zone of -3hs. The exception is Noronha that has -2hs in relation to Greenwich.

It should be noted that during summer time the Northeast, Southeast and South regions have the same zone of Noronha (-2hs).

  • You’re right, I’ll rephrase the question the answer is what I was looking for.

  • All with this mistake: [System.TimeZoneNotFoundException: The time zone ID 'Brazil/Acre' was not found on the local computer.] You know why?

  • As documented by MSDN https://msdn.microsoft.com/pt-br/library/system.timezoneinfo.id(v=vs.110). aspx

  • @rubStackOverflow you should change Timezoneinfo.Findsystemtimezonebyid("Brazil/West") to something like: Timezoneinfo.Findsystemtimezonebyid("E. South America Standard Time") //

Browser other questions tagged

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