Forcing a DateTime to be under a specific timezone, given only a DateTime with unspecified timezone
I have a date, say "2016-10-20 20:34:60". When I create this datetime, I'm not sure how to specify that this DateTime was made based on the servers local timezone. IN this case, it's "Pacific Standard Time".
I'm new to C#, and am trying to wrap my head around how to do so. Many of the examples given are using UTC time. We store dates based on the servers localtime.
I want to ensure that the displayed default text is correct, regardless if we are in daylight saving mode or not, it should reflect what it was at that moment in time.
You can use
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time"); DateTime newDateTime = TimeZoneInfo.ConvertTime(existingDateTime, timeZoneInfo);
DateTime vs DateTimeOffset, UTC and Local, there is absolutely no explicit time zone offset associated with it. Because datetimeoffset includes only the difference between UTC and local time with the saved datetime value; it does not include the time zone. Anyone who has to deal with different time zones is well-advised to learn the difference.
You can get the current time zone with
Alternatively, you can get the current offset like this:
You'd need to store the relevant info in the database along side your date and time.
EDIT Per the comment below and https://docs.microsoft.com/en-us/dotnet/standard/datetime/
TimeZone.CurrentTimeZone has been superseded. Use
TimeZoneInfo.Local instead. The answer was modified to reflect this.
How to translate UTC to your time | Astronomy Essentials, If you construct a DateTimeOffset from a DateTime with DateTimeKind.Unspecified, it will default to the local time zone. Similarly, if you create The point is that if you call ToLocalTime using an "unspecified" DateTime, then the value will be treated as if it were in UTC. Likewise if you call ToUniversalTime using an "unspecified" DateTime , then the value will be treated as if it were in the system local time zone.
In general, you shouldn't rely on the server's local time zone. It's a system-wide setting that can be easily changed without administrative permissions. Doing so makes you vulnerable to hard to track down production bugs, such as one server in a cluster having a different time zone setting than another. It also presents a problem if ever moving the server to a different location (or to the cloud) and having your data affected.
It's also very common for servers to have their time zones set to UTC intentionally, as to not have their log files affected by things like daylight saving time transitions.
If regardless you still want to assert that a
DateTime value reflected the computer's local time zone, you'd do that by ensuring
DateTimeKind.Local is set in the
.Kind property. This can be done in a constructor parameter, or with
DateTimeStyles.AssumeLocal when parsing, or with
DateTime.SpecifyKind on an existing value. You could then use
.ToUniversalTime() to convert to UTC before sending it along to the end user.
However, I strongly encourage you to use
DateTimeOffset instead. Unlike
DateTime, it will keep the offset around, and take it into consideration for most operations. If you construct a
DateTimeOffset from a
DateTimeKind.Unspecified, it will default to the local time zone. Similarly, if you create one via parsing (
.TryParseExact, etc.) if there is no offset in the input string, it will default to the local time zone also.
HOWEVER - none of these objects will actually track a time zone in a way that will properly take transitions into account (such as when daylight saving time moves Pacific Time between PST and PDT and vice versa). If that's what you're looking for - sorry, you're out of luck with the built-in types. Instead you'll need to use the
ZonedDateTime class from the Noda Time open-source library.
Also, you stated:
DateTimeOffset value back to browser will give you that for display, but if you're going to ask the user to change the value then you'll have to convert it back somehow.
It's all heck of a lot easier if you simply pass UTC values back and forth between the browser and the server. Then you can just convert UTC to Local time and vice versa in the browser. (At least for date+time values. Don't try to convert date-only values.)
DateTime.ToUniversalTime Method (System), DateTimes and DateTimeOffsets can be misparsed if the culture settings differ from those in force when formatting takes place. In this section, we examine in more detail how time zones influence DateTime and DateTimeOffset. DateTimeKind (Unspecified, Local, or Utc) When you compare two DateTime instances, only Note. Both the DateTime and the DateTimeOffset types have some limitations when representing times in time zones. With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone.
It not only represents a time zone but it also provides methods to check for a datetime validity: TimeZoneInfo tz = TimeZoneInfo.Local; // getting the I have a DateTime stored in universal time (UTC) of value 2010-01-01 01:01:01.. I would like to display it in EST in this format 2010-01-01 04:01:01GMT-04:00, however the 'K' formatter for timezone doesn't work in ToString
But it's surprisingly complex using date data effectively if you want to Web apps typically require that dates are stored in time zone agnostic fashion. convert UTC dates to a specific locale via some DateTime extension <summary> /// Returns TimeZone adjusted time for a given from a Utc or local time. If you construct a DateTimeOffset from a DateTime with DateTimeKind.Unspecified, it will default to the local time zone. Similarly, if you create one via parsing ( DateTimeOffset.Parse , .TryParse , .ParseExact , .TryParseExact , etc.) if there is no offset in the input string, it will default to the local time zone also.
Culture-insensitive date/time format strings Format string Meaning Sample output Notes o Will append time zone information unless DateTimeKind is Unspecified r,R RFC1123 In fact, "o" is the only format specifier in the group that can write an because the same settings are in force when parsing as when formatting. Active 4 years, 8 months ago. Viewed 23k times. 4. It seems that the datetime value which is entered into a DateTimeField is always stored as UTC no matter what timezone is configured for the current user. Only the output on the page is displayed in the correct timezone format.