Date is being converted back to local time and I want it to stay UTC RRS feed

  • Question

  • User-1188570427 posted

    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);

    Monday, April 15, 2019 3:28 AM

All replies

  • User-1188570427 posted

    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.

    Monday, April 15, 2019 3:35 AM
  • User-1188570427 posted

    I guess I have to take the local date and then convert it back to UTC:

    Just <g class="gr_ gr_42 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="42" data-gr-id="42">curiosu</g> 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);
                                return convertedPerLocalNodaTimeZoneDt;

    Monday, April 15, 2019 3:50 AM
  • User-893317190 posted

    Hi tvb2727,

    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 https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp

    Best regards,

    Ackerly Xu

    Monday, April 15, 2019 6:19 AM
  • User-987317448 posted

    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?

    Friday, September 25, 2020 7:24 PM
  • User303363814 posted

    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).

    Friday, September 25, 2020 11:31 PM