c# – Set time zone for the entire project

Question:

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

Another thing too, I would like to know if it is possible to define the Brazilian format for all dates once, without having to change the format in each variable.

Answer:

It's not possible. I don't even know if it's desirable. Timetables tend to work best like UTC in most situations and are only displayed or manipulated in specific situations like timezone .

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

The presentation of dates, not the time zone, can be made according to the crop setting. It has a few shapes. By code is used:

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

I put it on GitHub for future reference .

Scroll to Top
AllEscort