Set time zone for the whole project

Asked

Viewed 709 times

2

I recently hosted an application on a North American server, so the dates are outside the Brazilian time zone. In this way, I would like to know if there is a way to define the time zone once so that every time the DateTime.Now in a variable, its value is already updated according to the defined zone.

Another thing also, I wonder if it is possible to define a single Brazilian format for all dates, without having to change the format in each variable.

1 answer

1


It’s not possible. I don’t even know if it’s desirable. Times usually work better as UTC in most situations and only be presented or manipulated in specific situations such as Timezone.

What you can do to make it a little easier is to create a utility class with methods (possibly extension) that will give you back the time on Timezone desired (you would keep in this utility class a static property indicating which is the Timezone that you want and the methods would respect this without having to pass an argument saying which to use, although you can optionally allow having a parameter to choose a different one from the active pattern.

The date display, not the spindle, can be done according to the culture configuration. It has some forms. By code is used:

Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");

I put in the Github for future reference.

  • Well, that’s too bad. Already for the format I added this on web.config: <globalization enableClientBasedCulture="false" Culture="en-BR" uiCulture="en-BR" /> and it worked.

  • For format yes, but spindle has no way.

Browser other questions tagged

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