Quote:
as seen above last 2 row in datable is not in valid "dd-mm-yyyy" format
Really?
Take a look at below example:
List<string> lstOfDates = new List<string>()
{
"22-07-2024",
"23-09-2024",
"2024-09-23",
"1234-09-23"
};
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("De-de");
System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None;
foreach(string s in lstOfDates)
{
DateTime d;
if(DateTime.TryParse(s, out d) || DateTime.TryParse(s, ci, styles, out d))
Console.WriteLine($"{s} is valid date.");
else
Console.WriteLine($"{s} is NOT valid date.");
}
Result:
22-07-2024 is valid date.
23-09-2024 is valid date.
2024-09-23 is valid date.
1234-09-23 is valid date.
I'd suggest to read this:
DateTime.TryParse Method (System) | Microsoft Learn[
^]
Quote:
i want to check that datable date column contain valid date format
No problem. :)
One way to achieve that is to create a list of possible formats and use them with this method:
DateTime.TryParseExact Method (System) | Microsoft Learn[
^]
Final note:
A date format depends on
regional settings[
^] user preferences. I do like
yyyy-MM-dd
format, but my colleagues prefer
dd-MM-yyyy
and official regional setting in Poland is...
You can check this out by using below code:
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("PL-pl");
Console.WriteLine($"Short: {ci.DateTimeFormat.ShortDatePattern} \nLong: {ci.DateTimeFormat.LongDatePattern}");
More information you can find here:
Format dates and times - Globalization | Microsoft Learn[
^]