Last post Sep 25, 2020 11:31 PM by PaulTheSmith
Apr 15, 2019 03:28 AM|tvb2727|LINK
I have the below date format that is a string:
I want to keep it UTC, which that is above.
When I do DateTime.TryParse() it converts it to local time.
How do I keep it as UTC and place it in a DateTime object?
I tried this, but it returns false:
string format = "yyyy-MM-dd HH:mm:ss";
if (DateTime.TryParseExact(dateInfoValue, format, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out parsedDateTime) == true)
return convertedPerLocalNodaTimeZoneDt = NodaTimeHelpers.ConvertDateTimeToDifferentTimeZoneDt(parsedDateTime, systemNodaTimeZone, localNodaTimeZoneNm);
Apr 15, 2019 03:35 AM|tvb2727|LINK
I tried this as well:
Everything is converting it back to local time!
var myDateUtc = DateTime.SpecifyKind(DateTime.Parse(dateInfoValue), DateTimeKind.Utc);
I want it to stay UTC. I know the Z means that it is UTC, so I guess it is using my computer time zone to place it in central time.
Apr 15, 2019 03:50 AM|tvb2727|LINK
I guess I have to take the local date and then convert it back to UTC:
Just curiosu if there are other ways to do this in one statement?
if (getDateTimeInfoHtmlDoc != null)
var dateInfoValue = getDateTimeInfoHtmlDoc.GetAttributeValue("data-date", null);
if (dateInfoValue.IsStringNullOrEmptyOrWhiteSpace() == false)
if (DateTime.TryParse(dateInfoValue, out parsedDateTime) == true)
convertedPerLocalNodaTimeZoneDt = NodaTimeHelpers.ConvertToLocalTimeZoneFromUtcDateTime(parsedDateTime.ToUniversalTime()), localNodaTimeZoneNm);
Apr 15, 2019 06:19 AM|Ackerly Xu|LINK
If you want to keep timezone , you could also use DateTimeOffset.
Below is my test code.
Response.Write(DateTimeOffset.Parse("2012-10-08T04:50:12.0000000+08").UtcDateTime + "<br/>");
Response.Write(DateTimeOffset.Parse("2012-10-08T04:50:12.0000000+08").DateTime + "<br/>");
And the result.
Also refer to this link
Sep 25, 2020 07:24 PM|M1iS|LINK
I get so sick of fighting with .NET over converting UTC to local, such a crap "feature." Stop it! Who writes business applications that doesn't store dates as UTC?
Sep 25, 2020 11:31 PM|PaulTheSmith|LINK
Who writes business applications that doesn't store dates as UTC?
It is so much easier when you are looking at the raw data in the database to have the timezone be the 'most useful' timezone. I always use the Timezone of Head Office. When I look at some DateTime value in the database I don't want to have to remember
when the daylight saving change occurred a few years ago.
My suggestion is that if the DateTime data type is not suitable for you then you should not use it! DateTimeOffset is intended for use when timezones are involved (there is a hint in the name).