System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY

24,567

Solution 1

return DateTime.ParseExact(dateInString, "M/d/yyyy", CultureInfo.InvariantCulture);

Check it here

The difference between my answer and Fabulous' one is that I also shortened dd to d so if your user writes 6/6/2018 it will work too

Solution 2

Your date format is MM/dd/yyyy and your input doesn't match. It is M/dd/yyyy

Based on your comment, to solve the 6/1/2018 issue, you'll need to do it as follows M/d/yyyy

Solution 3

Your format string is missing the AM/PM deisgnator:

return DateTime.ParseExact
    (dateInString + " 12:00:00 AM", "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
    // Here ---------------------------------------------^

Solution 4

Try without concatenation with string " AM"

Solution 5

I was getting the same exception a couple of days ago when it was 9th of July, I simply appended a 0 with 9 to match the date format.

Try appending a 0 in your month to match the MM in your date format

Share:
24,567
Aishwarya Shiva
Author by

Aishwarya Shiva

With over 10 years of experience providing thorough and skillful advice in software development, digital marketing, PC troubleshooting and other tech related issues to both individuals and businesses. Use this VIP link and give me a call for free .NET, C#, ASP.NET, MVC, JAVA, PHP, WCF, WPF etc. consultation: https://clarity.fm/aishwaryashiva/early638

Updated on July 14, 2020

Comments

  • Aishwarya Shiva
    Aishwarya Shiva almost 4 years

    I have following C# method:

    DateTime ConvertStringToDate(string dateInString)
    {
        try {
            //string SSD = dateInString;
            //DateTime date = Convert.ToDateTime(SSD);
            //string strDate = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", date);
            //return Convert.ToDateTime(strDate);
    
            return DateTime.ParseExact(dateInString, "MM/dd/yyyy", CultureInfo.InvariantCulture);
        }
        catch (Exception) { }
        return DateTime.Today;
    } 
    

    The code in comment is another way I tried before.

    I am in India and developing an ASP.NET WebForms application for my client in US. On one of its forms my client will enter the date in TextBox like 6/20/2018 which is MM/dd/yyyy format.

    But in both the ways I am getting this error: System.FormatException: 'String was not recognized as a valid DateTime.'

    I tried many solutions on SO but none of them are working.

    • Aishwarya Shiva
      Aishwarya Shiva almost 6 years
      @Rafalon Sorry I tried to test it using time. Forgot to remove it. It's also not working for date only.
    • mjwills
      mjwills almost 6 years
      What is the exact value of dateInString? Don't guess, debug it and check.
    • Rafalon
      Rafalon almost 6 years
      MM/dd/yyyy format would require your date to be 06/20/2018 and still MM/dd/yyyy hh:mm:ss can't work for date-only strings
    • Aishwarya Shiva
      Aishwarya Shiva almost 6 years
      @mjwills the value is 6/20/2018 when debugging.
    • Rafalon
      Rafalon almost 6 years
      Then how do you expect MM/dd/yyyy hh:mm:ss to match this?
    • Aishwarya Shiva
      Aishwarya Shiva almost 6 years
      @Rafalon oh sorry i again forgot to remove the time from format. I tried only date too in the format before.
  • Aishwarya Shiva
    Aishwarya Shiva almost 6 years
    Sorry I tried to test it using time. Forgot to remove it. It's also not working for date only.. See the update.
  • Aishwarya Shiva
    Aishwarya Shiva almost 6 years
    Sorry I tried to test it using time. Forgot to remove it. It's also not working for date only. See the update.
  • Dmitriy S
    Dmitriy S almost 6 years
    You fill out MM/dd/yyyy but use this "MM/dd/yyyy hh:mm:ss" pattern
  • Dmitriy S
    Dmitriy S almost 6 years
    Try to use "MM/dd/yyyy" only
  • Aishwarya Shiva
    Aishwarya Shiva almost 6 years
    Tried this format for "6/1/2018" but it's not working.
  • Rafalon
    Rafalon almost 6 years
    @AishwaryaShiva yes it does
  • Fabulous
    Fabulous almost 6 years
    @Rafalon after seeing the OP's comment and sorting this, I saw and upvoted your answer. I had not seen it when I was editing my original answer which referenced the time.
  • mason
    mason almost 6 years
    @AishwaryaShiva Why would you ask that. Why wouldn't you just test it?
  • Rafalon
    Rafalon almost 6 years
    @mason I actually asked myself the same question as you did, especially since I included a link where you can basically change the input and hit "Execute"
  • Aishwarya Shiva
    Aishwarya Shiva almost 6 years
    @mason I was actually in a hurry. So read the answer without noticing the link and the example. ;)