Setting the default time zone for DateTimes

I have a C # application that will work in a different time zone than where I develop it. It uses time data and performs calculations and makes decisions based on time data. I want to test using real data, so I wondered how I can set the time zone or something similar in the application, as if I were in a different time zone, and all variables of the DateTime type will use this time by default?

+4
source share
4 answers

The easiest way is to simply change the time zone on your computer that receives the DateTime class. As a general recommendation, I will do all the time associated with calculations and storing time in UTC, only for display purposes, convert to the local time zone.

+5
source

DateTime has only three β€œmodes”: DateTimeKind.Utc , DateTimeKind.Unspecified and DateTimeKind.Local . It does not calculate based on time zone. This does not compensate for this in any way. There is a DateTimeOffset that can help you (starting with .NET 2.0 SP2)

+2
source

Is the DateTimeOffset class available? (this is not when you use the compact framework). Judging by its API, it seems to very well reflect your requirements.

+1
source

You will need to set the time zone for the computer on which the application is running.

0
source

Source: https://habr.com/ru/post/1340695/


All Articles