-2

I have a date time column named ProjectLastUpdate which has values such as 2/22/2020 11:29:52 PM & 1/29/2020 12:00:00 AM..

I wrote the following code:

DateTime.ParseExact(projectLastUpdate.ToString(), "M'/'d'/'yyyy' 'H':'m':'s", CultureInfo.InvariantCulture).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")

to convert the date to the ISO standard, but I am getting this exception:

String was not recognized as a valid DateTime

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John John
  • 1
  • 72
  • 238
  • 501
  • Why are you going around in circles. You have a DateTime. You are converting to a string (with a no argument call to ToString), then trying to parse that string back to a DateTime, specifying the exact format you expect, and then converting it back to a DateTime again. If you were to unroll thing into several statements to tell where the error occurred, I would expect it's on the ParseExact call. Your default call to ToString likely doesn't match the exact format you specify – Flydog57 Feb 23 '20 at 00:07
  • @Flydog57 i have to do it in this way as the values i will get will be as follow `2/22/2020 11:29:52 PM`.. so i need to make this string as an understandable DateTime so i used `DateTime.ParseExact(projectLastUpdate.ToString(), "M'/'d'/'yyyy' 'H':'m':'s", CultureInfo.InvariantCulture)` after that i convert the recognized Datetime into the ISO standard using `.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")` – John John Feb 23 '20 at 00:10
  • 3
    If you have a DATETIME field (the column datatype is DATETIME), then you don't need to convert it to a string and parse it out to turn it back to a date. Just read it directly from the table as a DATETIME. – Ken White Feb 23 '20 at 00:24
  • @KenWhite the value `2/22/2020 11:29:52 PM` will be returned as a string – John John Feb 23 '20 at 00:35
  • No, if you have a DATETIME column in the table, then what you retrieve is a DATETIME. If you're getting something else, you either don't have a DATETIME column or you're doing a conversion (which you're then trying to convert back with the code you posted). – Ken White Feb 23 '20 at 00:36
  • @KenWhite so what i need to do to fix this? – John John Feb 23 '20 at 00:37
  • Use `ProjectLastUpdate.ToString()`. Period. Nothing else. You don't need to do `ToString()` and then parse it back into a datetime again. – Ken White Feb 23 '20 at 00:40
  • 1
    If you need it to *display* an ISO format use an ISO argument like "o" or "s". See **[Standard date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings)** which was alluded to sometime ago in the first comment – Ňɏssa Pøngjǣrdenlarp Feb 23 '20 at 00:43
  • @KenWhite i got lost on what i need to do? now as i know the `2/22/2020 11:29:52 PM` can not be directly converted into `"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"` DateTime? – John John Feb 23 '20 at 00:57

1 Answers1

1

The format parameter of ParseExact function is not correctly specified. You can change the statement as:

DateTime.ParseExact(projectLastUpdate.ToString(), "M/dd/yyyy hh:mm:ss tt",
                                                   CultureInfo.InvariantCulture)
                    .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");

Based on your code-snippet it seems projectLastUpdate is in string format. If so, you could have avoided ToString() conversion.

MKR
  • 19,739
  • 4
  • 23
  • 33
  • i tried your code but still i am getting the same exception `String was not recognized as a valid DateTime.`.. here are sample of the `projectLastUpdate` `7/27/2022 11:00:00 PM | 2/23/2020 12:00:00 AM |7/27/2022 11:00:00 PM`.. also the `projectLastUpdate` is of type object, so i need to convert it to a string.. – John John Feb 23 '20 at 17:51
  • Is it true that there are many Date time in `projectLastUpdate`? If so you have to separate it and then parse. You can use split to separate in multiple datetime. – MKR Feb 23 '20 at 18:28
  • no it only contain a single date time.. i just show samples of the datetime i will get.. sorry for the inconsistency – John John Feb 23 '20 at 18:30
  • Ahh okay. Can you send few more datetime text from your data. There might be one exception. Moreover are you performing this in a loop? – MKR Feb 23 '20 at 18:38
  • the 3 datetime i showed caused an exception.. so the problem can be found inside these 3 datetime – John John Feb 23 '20 at 18:38
  • @testtest Okay. I think you should look carefully in those string. Any additional space will cause problem. So one space gap between year and hour, the second and PM. And after PM there should not be any space. – MKR Feb 23 '20 at 19:01
  • i am sure the datetime does not have any empty chars – John John Feb 23 '20 at 21:51