简体   繁体   中英

string not recognized as a valid datetime in c# ERROR

Even after using DateTime.ParseExact I am getting error as

string not recognized as a valid datetime in c#

Below is my code

string strIDODDate = DateTime.ParseExact(ObjIp.ID_ODchangeDate, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");

Here is full set of code

string strRFCsDate = DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
        //string strRFCsDate1 = DateTime.ParseExact("25-09-2019 00:00:00.000", "dd-MM-yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)
        //              .ToString("dd-MM-yyyy"); 

        if (string.IsNullOrEmpty(ObjIp.ID_ODchangeDate))
        {
            Tobj.ID_ODchangeDate = strRFCsDate;
        }
        else
        {
            //Tobj.ID_ODchangeDate = Convert.ToString(ObjIp.ID_ODchangeDate);                

            string strIDODDate = DateTime.ParseExact(ObjIp.ID_ODchangeDate, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
            Tobj.ID_ODchangeDate = strIDODDate;
        }

update

After debugging, I found out that the format which was coming was with exception was below

10/28/2021 5:34:35 AM : 10/7/2019 12:00:00 AM 10/28/2021 5:34:35 AM : Error : Dumping into Table Process : String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)

Unclear from your question, so I'm assuming your incoming date time string is something like 25-09-19 00:00:00.000

var inDateTime = "25-09-19 00:00:00.000";
string parsedDateTime = DateTime.ParseExact(inDateTime, "dd-MM-yy hh:mm:ss.fff", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Console.WriteLine(parsedDateTime);

Output

25-09-2019

UPDATE:

Please review: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

These are the date and time format strings for parsing date times.

It's still unclear if your value is October 7 or July 10... Assuming July 10:

using System;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        var inDateTime = "10/7/2019 12:00:00 AM";
        string parsedDateTime = DateTime.ParseExact(inDateTime, "dd/M/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
        Console.WriteLine(parsedDateTime);
    }
}

Output

10-07-2019

See: https://dotnetfiddle.net/2YMb82

Based on your comments and update:

ObjIp.ID_ODchangeDate was coming in 10/7/2019 12:00:00 AM

You are telling ParseExact a your format is "dd-MM-yyyy hh:mm:ss tt" this means a few things:

  1. Your day will ALWAYS have 2 digits, meaning a leading 0 (you have 10)
  2. Your month will ALWAYS have 2 digits, meaning a leading 0 (you have 7)
  3. Your Year will have 4 digits (you have 2019)
  4. Your separator will be '-' (you have /)
  5. You will be using the 12 hour clock, including seconds (you have 12:00:00 AM)

The example you gave is not true for 2 and 4 of my list. I expect it won't be true for 1 for the first 9 days of any month, because of this you need to specify a format that doesn't include leading 0s and uses the correct date separators. The format you want is "d/M/yyyy hh:mm:ss tt" this format tells the parser:

  1. The day will have 2 digits if it needs them, no guaranteed leading 0
  2. The month will have 2 digits if it needs them, no guaranteed leading 0
  3. The year will have 4 digits
  4. The separator will be '/'
  5. The time will be using the 12 hour clock, including seconds

That means the code will look like this. I pulled the formats out into variables for readability, and turned object.property into a variable to make it run simply in fiddle.

string ObjIp_ID_ODchangeDate = "10/7/2019 12:00:00 AM";
string dtFormatIn = "d/M/yyyy hh:mm:ss tt";
string dFormatOut = "dd-MM-yyyy";
string strIDODDate = DateTime.ParseExact(ObjIp_ID_ODchangeDate, dtFormatIn, CultureInfo.InvariantCulture).ToString(dFormatOut);
Console.WriteLine(strIDODDate);

As you can see, this works for your case .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM