Hussain AlShaikh

Hussain AlShaikh

  • 1.7k
  • 27
  • 855

date evaluation

Apr 29 2024 4:12 PM

Hi

I have a problem in date evaluation

the windows setting on US dateformat mm/dd/yyyy

The input box on the page is text dd/MM/yyyy

DateTime StartDate;
DateTime EndDate;
                
                string sDate = TrinData[4].ToString(); //TEXTBOX ON PAGE (10/05/2024)
                string eDate = TrinData[5].ToString(); //TEXTBOX ON PAGE (15/05/2024)
                string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "dd/MM/yyyy" };

                if (!DateTime.TryParseExact(sDate, formats, new CultureInfo("en-US"),
                                            DateTimeStyles.None, out StartDate))
                {
                    Console.Write("The DateTime is in another format.");
                }
                if (!DateTime.TryParseExact(eDate, formats, new CultureInfo("en-US"),
                                                                    DateTimeStyles.None, out EndDate))
                    {
                        Console.Write("The DateTime is in another format.");
                    }

                

                if (StartDate.Date > EndDate.Date)
                {
                    lblMessage.Text += "The Ending Date must be grater or equal to starting date ";
                }

 

IT WILL RETURN THE FOLLOWING MSG.

The Ending Date must be grater or equal to starting date 

but if windows on English Date e.i. dd/MM/yyyy then everything works fine.

what did I miss?

Thank you

 


Answers (4)