-1

So I want to convert a certain UTC DateTime string that is in the following format 2020-04-18T20:05:00.000-04:00.

I am trying to use the following function :

DateTime.ParseExact(segment.DepartureDate,"yyyy-MM-ddTHH:mm:ss.fffzzz", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);

for converting the above Date string that i received from an API, to a C# DateTime type variable.

But the Actual Result is {19-04-2020 00:05:00}. Somehow the date gets rounded up or converted which is not what i am expecting.

Have also tried Date.Parse but that filters out the UTC Offset whose value i really need.

Can anyone suggest a legit way to achieve this conversion accurately.Thanks in Advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Gurunath Rao
  • 105
  • 1
  • 8
  • 5
    rounded? **it's expected value** *AdjustToUniversal - Date and time are returned as a Coordinated Universal Time (UTC).* ... the time is in zone -4 so for UTC you have to add 4 h ... (20:05 + 4h = 00:05 next day) and this is the result so ... **Where is the problem?** – Selvin Mar 10 '20 at 10:40
  • 1
    Be aware, `DateTime` does _not_ have UTC Offset value and it is too normal to lost that value after a parsing operation. That's why it would be better parse to `DateTimeOffset` instead. – Soner Gönül Mar 10 '20 at 10:44
  • Please check out [this answer](https://stackoverflow.com/a/18465222/1016343), it shows how you can convert any string to DateTime using TryParseExact. Use the extension method there, e.g. `dtStr.ToDate("yyyy-MM-ddTHH:mm:ss.fffzzz")` and check if it works for you. – Matt Mar 10 '20 at 10:44
  • Thanks @SonerGönül for the assisst. – Gurunath Rao Mar 10 '20 at 10:50
  • Why do you want to System.Globalization.DateTimeStyles.AdjustToUniversal? DateTime is stored as UTF automatically. When a timezone is not specified the Net library make an adjustment when storing using the timezone of the machine. So adjusting when you already have a timezone in the date is just confusing. When converting to a string the timezone of the machine is used as the default. Converting to another timezone is only needed when you do not want to use the default timezone setting of the machine. – jdweng Mar 10 '20 at 10:58
  • 2
    @jdweng: That comment is very confusing and inaccurate in various places. For example: "When converting to a string the timezone of the machine is used as the default" That's not true. The value is converted to the default *calendar* system of the current culture, but no time zone conversion is performed. For example, if you have a `DateTime` with a `Kind` of UTC, then converting it to a string will - by default - print the UTC value. Some format strings convert to UTC if the kind is `Unspecified` or `Local`; I don't think any formats will convert to the local time zone. – Jon Skeet Mar 10 '20 at 12:06
  • @Jon Skeet : The current culture and the machine timezone settings are the same. – jdweng Mar 10 '20 at 12:12
  • @jdweng: No, they're absolutely not. `CultureInfo` doesn't have a time zone at all, but it *does* have a default calendar. Calendar systems and time zones are very, very different. – Jon Skeet Mar 10 '20 at 13:07

1 Answers1

0

If you are working with different time zones (where the offset is not equal to your current locale's offset) and the offset is important, it is usually better to use DateTimeOffset instead of DateTime. DateTime only has a property identifying it as UTC or (system-)local, but DateTimeOffset stores the full offset value instead.

PMF
  • 14,535
  • 3
  • 23
  • 49