0

That question may be already been answered here, but I could not find any answers. I'm trying to to convert the string 25APR18 to 2018-04-25.

I tried to do the following:

DateTime.ParseExact("25APR18", "yyyy-mm-dd", CultureInfo.InvariantCulture);

But it does not convert and gives me the error saying "String was not recognized as a valid DateTime."

I was not able to find the right solution.

Any suggestions?

gene
  • 2,098
  • 7
  • 40
  • 98
  • 2
    There *are* a lot of answers, all of which show how to use `DateTime.ParseExact`. There are no dashes in this string, so why use `yyyy-mm-dd`? – Panagiotis Kanavos May 07 '18 at 14:28
  • I will look into that as well. Thx – gene May 07 '18 at 14:28
  • The values you posted use the `ddMMMyy` format – Panagiotis Kanavos May 07 '18 at 14:28
  • @PanagiotisKanavos I think you are missing an `M`. it should be `ddMMMyy` – Zohar Peled May 07 '18 at 14:29
  • @ZoharPeled oops – Panagiotis Kanavos May 07 '18 at 14:30
  • 3
    Does 25APR18 look like it's in the format yyyy-MM-dd? – ProgrammingLlama May 07 '18 at 14:30
  • For parsing you need to specify the format that the string is currently in. Then you can use `ToString` and the format you desire to get the correct output. – juharr May 07 '18 at 14:30
  • 6
    It's fascinating how many uses `ParseExact` without understanding the nature of the word `Exact`. The right format for that date string would be `"ddMMMyy"`, tested and works. – Lasse V. Karlsen May 07 '18 at 14:30
  • 1
    DateTime don't store display format. Only string representations of datetime have a display format. So when using `DateTime.ParseExact`, the format you specify is the display format of the string you want to parse. – Zohar Peled May 07 '18 at 14:31
  • @LasseVågsætherKarlsen most people never even have to think about how complicated dates and times can get. No wonder inexperienced developers makes mistakes with datetime that would be vexing to experienced developers. – Zohar Peled May 07 '18 at 14:33
  • Unfortunately, the top 3 things we deal with in our programs are also the top 3 most difficult things to get right, and that would be dates, text, and numbers (floating point numbers to be more specific). – Lasse V. Karlsen May 07 '18 at 14:38
  • I'm not sure these are the top 3 most difficult, but these are definitely the top 3 that are difficult while deceivingly looking easy. I mean, as humans, we deal with dates, texts and numbers every day and we don't even have to think about it, but as soon as you need to write a program that deals with 1/3 as a floating point number or translate one time zone to another while crossing from daylight savings to non-daylight savings - that's when things gets really nasty... – Zohar Peled May 07 '18 at 14:47

2 Answers2

6

You can use:

DateTime date = DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);

The string format is case sensitive representing the input format you want to match. Lower case mm represents minutes but you need month abbreviated name MMM and that's one issue with the code you posted. Full list of format specifiers can be found here.

Once you have a DateTime object you can retrieve the date as string with new format:

string formattedDate = date.ToString("yyyy-MM-dd");

Which produces:

2018-04-25

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • I tried that option already. It gives me the same error that the string cannot be recognized as valid DateTime – gene May 07 '18 at 14:35
  • 2
    While the code is probably correct, this answers lacks explanations. It's no more then a simple code dump. Please try to elaborate on what's the reason this code is correct while the code in the question is wrong. – Zohar Peled May 07 '18 at 14:36
  • 1
    @gene No it didn't give you that error. – ProgrammingLlama May 07 '18 at 14:36
  • [Proof here](http://rextester.com/UUI34536) – ProgrammingLlama May 07 '18 at 14:37
  • @john sorry, was answering another post. However, your suggestion convert to 4/25/2018 12:00:00 AM and I need 2018-04-25 – gene May 07 '18 at 14:39
  • 1
    @gene that is incorrect. It converts to a `DateTime` object. A `DateTime` object stores the datetime as a number and thus has no human-readable representation. See my [answer here](https://stackoverflow.com/questions/50065387/datetime-convert-to-yyyy-m-d-anyway-converts-to-m-d-yyyy/50065742#50065742) for a better explanation. – ProgrammingLlama May 07 '18 at 14:40
  • 1
    That's better. Now I can upvote your answer. – Zohar Peled May 07 '18 at 14:42
0

Your date parse code should be

DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);