Click here to Skip to main content
16,022,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following piece of source code in a block of a select statement.
C#
DateTime dt_ = DateTime.Parse(ds.Tables[0].Rows[i][ColName].ToString());
string  dateString = dt_.ToShortDateString().ToString();
DateTime date = Convert.ToDateTime(dateString,                                            System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

dateString = date.ToString("yyyy-MM-dd");

InvoiceDate_ = DateTime.ParseExact(dateString,
"yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);


Everything seams to work fine during runtime, with a date like this:
dt_ = {8/4/2024 12:00:00 πμ}
But with another value like the following:
dt_ = {13/9/2024 12:00:00 πμ} 

I am getting error in the statement
DateTime date = Convert.ToDateTime(dateString,                                            System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

that the string isn't recognized as a valid DateTime.
Explanation: "πμ" is written a greek and it means before noon and I see it in all DateTime Values.

I don't understand the reason that works with one and not the other date value. Any help and explanation will be much appreciated.
Thank you so much in advance.

What I have tried:

I tried the following but it doesn't work for none of the date values.

DateTime test;
     if( DateTime.TryParseExact(dateString, "dd/mm/yyyy", CultureInfo.GetCultureInfo("en-US").DateTimeFormat, DateTimeStyles.None, out dtTarget))
      {
           test = dtTarget.Date;
      } 
Posted
Updated 13-Sep-24 5:41am
v2

1 solution

You are explicitly telling the system to parse the date using US rules.

US dates are formatted as M/d/yyyy. Therefore, you are trying to parse value representing 9th Smarch[^] - the fictional thirteenth month, which doesn't exist.

The previous value "worked", in that it parsed the date. But it didn't give you what you were expecting: it would produce 4th August, not 8th April.

DateTime.TryParseExact didn't work, because the format you've told it to parse doesn't match the format of the input. You've told it to parse dd/mm/yyyy, but the input is formatted as dd/M/yyyy hh:mm:ss "πμ". If you change your code to:
C#
DateTime.TryParseExact(dateString, "dd/M/yyyy hh:mm:ss \"πμ\"", CultureInfo.GetCultureInfo("en-US").DateTimeFormat, DateTimeStyles.None, out dtTarget)
it should parse successfully.

However, it would probably be easier to use the correct culture to parse the dates instead:
C#
DateTime.TryParse(dateString, CultureInfo.GetCultureInfo("el-GR"), DateTimeStyles.None, out dtTarget)
 
Share this answer
 
Comments
Aggeliki Asimakopoulou 13-Sep-24 12:16pm    
Your are definitely right. This conversion was absolutely wrong.
The right one is
DateTime date = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("el-GR").DateTimeFormat);
I didn't know that. Thank you so much for the explanation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900