Change the Datetime Timezone.

Asked

Viewed 13,485 times

14

I am hosting my system on a server that is in the USA.

So when using the DateTime.Now returns the date and time of the US.

I would like you to return the date and time of Brazil. It is possible?

3 answers

20


You need to convert to the desired spindle thus:

TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time"))

Behold working in the .NET Fiddle. Also put on the Github for future reference. Note that it is hosted in another country but sets the server to universal time. It does not matter in this case because the conversion made on top of local time considering the zone that is actually being used.

I don’t think so but see if there are any spindles more suitable for you.

You can create a method to pick up local time:

public DateTime PegaHoraBrasilia() => TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time"));

Behold working in the .NET Fiddle. And in the Coding Ground. Also put on the Github for future reference.

Give the name you think most appropriate. And call this method whenever you want the DateTime.Now on Brasilia time.

4

If you don’t have access to the server settings (to change its time zone) you can use the following method to convert the time

public DateTime HrBrasilia()
        { 
            DateTime dateTime = DateTime.UtcNow;
            TimeZoneInfo hrBrasilia = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time");
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, hrBrasilia);
        }

Complementing you could add the code below in your Web.config:

<configuration>
  <system.web>
    <globalization culture="pt-BR"/> // ADICIONAR ESTA LINHA AO SEU Web.config
  </system.web>
</configuration>

So that the date formats are also in the standard used by Brazil.

Sources:

  • Hence in all my code I would use Converterhora(Datetime.Now), rather than just Datetime.Now ?

  • @Diegozanardo made an issue in the reply, as it would be only for 'Datetime.Now' the method could be like the answer above and you call it 'Hrbrasilia()' (or any other name you want). The best method to solve this problem would be even you have access to the date/time settings of your server and change them to the time zone you want.

  • 1

    @Diegozanardo This very thing. Although it has a slightly better way of constituting the method, I and williamhk2 think about it together.

3

In addition to Maniero’s response, it’s important to remember to use Globalization to set your regional settings in case your code (and interface codes) need specific settings for pt_br.

A simple alternative is to ask the user which Time zone is right for him and allow him to select language and location options. But, it depends a lot on the purpose of the application.

Generic or embedded applications generally control this to the taste of the user.

Link about Globalization: http://msdn.microsoft.com/en-us/goglobal/bb688125.aspx

Browser other questions tagged

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