locked
String was not recognized as a valid DateTime RRS feed

  • Question

  • User-240513752 posted


      DateTime CheckInDate = DateTime.ParseExact(txtCheckIn.Text, "yyyy-MM-dd HH:mm:ss,fff",
                                            System.Globalization.CultureInfo.InvariantCulture);
    here txtCheckIn.Text =9/9/2015 12:00:00 AM


    run time error occuring "String was not recognized as a valid DateTime. "

    Friday, September 11, 2015 10:24 AM

Answers

  • User2103319870 posted

    Khan_1

    DateTime.ParseExact(txtCheckIn.Text, "yyyy-MM-dd HH:mm:ss,fff",

    You are using wrong date format in DateTime.ParseExact method. when using ParseExact you need to pass  the date input as exact format which we mentioned in code

    Try with the below code

    //Convert the text value to Datetime
    DateTime CheckInDate = DateTime.ParseExact("9/9/2015 12:00:00 AM", "M/d/yyyy h:mm:ss tt",System.Globalization.CultureInfo.InvariantCulture);
    //Your Required format
    string reqval = CheckInDate.ToString("yyyy-MM-dd HH:mm:ss,fff");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 10:35 AM
  • User-484054684 posted

    Hi, your string is not in the same format as you have defined. There is mismatch.

    See from MSDN documentation as below (So you need to make sure the dateString (in your case txtCheckIn.Text) matches with the format.

    // Parse date and time with custom specifier.
          dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
          format = "ddd dd MMM yyyy h:mm tt zzz";
          try {
             result = DateTime.ParseExact(dateString, format, provider);
             Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
          }
          catch (FormatException) {
             Console.WriteLine("{0} is not in the correct format.", dateString);
          }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 10:36 AM

All replies

  • User2103319870 posted

    Khan_1

    DateTime.ParseExact(txtCheckIn.Text, "yyyy-MM-dd HH:mm:ss,fff",

    You are using wrong date format in DateTime.ParseExact method. when using ParseExact you need to pass  the date input as exact format which we mentioned in code

    Try with the below code

    //Convert the text value to Datetime
    DateTime CheckInDate = DateTime.ParseExact("9/9/2015 12:00:00 AM", "M/d/yyyy h:mm:ss tt",System.Globalization.CultureInfo.InvariantCulture);
    //Your Required format
    string reqval = CheckInDate.ToString("yyyy-MM-dd HH:mm:ss,fff");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 10:35 AM
  • User-484054684 posted

    Hi, your string is not in the same format as you have defined. There is mismatch.

    See from MSDN documentation as below (So you need to make sure the dateString (in your case txtCheckIn.Text) matches with the format.

    // Parse date and time with custom specifier.
          dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
          format = "ddd dd MMM yyyy h:mm tt zzz";
          try {
             result = DateTime.ParseExact(dateString, format, provider);
             Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
          }
          catch (FormatException) {
             Console.WriteLine("{0} is not in the correct format.", dateString);
          }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 10:36 AM