0

I want to convert a string of date and time to DateTime structure, but it is giving this error :

String was not recognized as a valid DateTime

DateTime dt = Convert.ToDateTime("5/15/2018 11:54:18 AM");       
string date= dt.ToString("HH:mm");

I'm reading this question but I can't solve this code. What is my mistake?

What is the difference between Convert.ToDateTimeand DateTime.ParseExact() in C#?

Mohammad Daliri
  • 1,370
  • 6
  • 20
  • 43
  • 4
    I would suggest using `DateTime.ParseExact` or `DateTime.TryParseExact`, specifying the *exact* format of your input. – Jon Skeet May 24 '18 at 20:54
  • 3
    What @DaisyShipton said about parsing, but note that your variable names doesn't line up with your intent, you're doing `.ToString("HH:mm")` which is minutes and seconds, but you're naming the variable `date`, this is a recipe for disaster later as you're quick to forget that your code doesn't do what it seems to be doing. You should strive to write proper names that accurately represent the content and intent of the values they hold. – Lasse V. Karlsen May 24 '18 at 20:56
  • Always use `DateTime.TryParse()` or `DateTime.TryParseExact()`, although this format is common enough where `TryParse()` would likely be enough – maccettura May 24 '18 at 21:03
  • 1
    I was able to run your piece of code with no errors, can you provide more details on the type of environment you are working on (ex: .net framework version, OS, etc...) – Vijay May 24 '18 at 21:05
  • @Adi I'm using .net framework 4.5 – Mohammad Daliri May 24 '18 at 21:08
  • 2
    `TryParseExact` is the way to be sure, but probably what is happening is that your current culture settings is expecting day/month rather than month/day – Matt Burland May 24 '18 at 21:08
  • Hope this link will helps you [Converting a string to datetime](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime) – Sreevardhan May 24 '18 at 21:11
  • The question you quoted and comments here tell you to use `DateTime.ParseExact`, so why didn't you do that? – Dour High Arch May 24 '18 at 21:20
  • Possible duplicate of [How convert string to Datetime by a format?](https://stackoverflow.com/questions/14103970/how-convert-string-to-datetime-by-a-format) – mjwills May 24 '18 at 21:39
  • Possible duplicate of [String was not recognized as a valid DateTime " format dd/MM/yyyy"](https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy) – Tetsuya Yamamoto May 25 '18 at 01:03

2 Answers2

3

Based on all the comments, here's how your code should look like

DateTime dt = DateTime.ParseExact("05/15/2018 11:54:18 AM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
string date = dt.ToString("HH:mm");
Vijay
  • 426
  • 1
  • 5
  • 15
0

What is my mistake?

You Mistake is You are Providing argument Convert.ToDateTime() in a wrong Format. try Providing "DD/MM/YYYY HH:MM:SS" according to Your system date Time Format .Else you need to use TryParseExatct with Specifying Format

Debashish Saha
  • 318
  • 1
  • 12
  • What's wrong with the format? It works fine for me as it is in the example (I'm in `en-US` culture) – Rufus L May 24 '18 at 21:24
  • Okay.Provide Time in 24HR Format.Are you still getting Exception ? – Debashish Saha May 24 '18 at 21:25
  • I'm not getting an exception. Are *you* getting an exception? Does your suggestion work for you? – Rufus L May 24 '18 at 21:26
  • Okay.Something weird I found .I changed my system timezone as well as date format and the code worked .How is it possible? – Debashish Saha May 24 '18 at 21:35
  • 1
    The code as posted in the question will work for cultures that expect MM/dd , but will fail if the culture expects dd/MM(because there aren't 15 months). That may be it? – Rufus L May 24 '18 at 22:42