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)
CodePudding user response:
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
CodePudding user response:
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:
- Your day will ALWAYS have 2 digits, meaning a leading 0 (you have 10)
- Your month will ALWAYS have 2 digits, meaning a leading 0 (you have 7)
- Your Year will have 4 digits (you have 2019)
- Your separator will be '-' (you have /)
- 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:
- The day will have 2 digits if it needs them, no guaranteed leading 0
- The month will have 2 digits if it needs them, no guaranteed leading 0
- The year will have 4 digits
- The separator will be '/'
- 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.