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
Prasad RaveendranPosted May 1, 2024, 1:09 AM
try the below code
In this approach, the
TryParseDatemethod iterates over each date format in thedateFormatsarray until it successfully parses the date usingDateTime.TryParseExact. If parsing succeeds, it returnstrue; otherwise, it returnsfalse. This method encapsulates the parsing logic, making the main code block cleaner and easier to understand.Jayraj ChhayaPosted Apr 30, 2024, 11:18 AM
The issue you are facing is related to the mismatch between the date format in the input box and the date format specified in the
DateTime.TryParseExactmethod. The method is trying to parse the dates using the formats specified in theformatsarray, but it is not able to match the input dates with any of the formats provided.To resolve this issue, you need to update the
formatsarray to include the correct format for the input dates. In your case, the correct format for the input dates is "dd/MM/yyyy". You can modify your code as follows:By including the "dd/MM/yyyy" format in the
formatsarray, theDateTime.TryParseExactmethod will be able to parse the input dates correctly.Additionally, make sure that the
CultureInfoparameter in theDateTime.TryParseExactmethod is set to the appropriate culture that matches the input date format. In your case, you are using the "en-US" culture, which is correct for the US date format.Once you make these changes, the
DateTime.TryParseExactmethod should be able to parse the input dates correctly, and the comparison between the start and end dates should work as expected.Naimish MakwanaPosted Apr 30, 2024, 10:18 AM
The issue you’re facing is due to the difference in date formats. Your input box on the page is in “dd/MM/yyyy” format, but your Windows setting is in “MM/dd/yyyy” format.
When you’re parsing the date using
DateTime.TryParseExactwith the “en-US” culture, it expects the date in “MM/dd/yyyy” format. But since your input is in “dd/MM/yyyy” format, it’s interpreting the day as the month and vice versa.For example, the date “10/05/2024” is being interpreted as October 5, 2024 instead of May 10, 2024. Hence, when you compare the start date (October 5, 2024) with the end date (March 15, 2024), it’s showing that the end date is earlier than the start date.
To fix this, you can create a custom
CultureInfothat uses the “dd/MM/yyyy” format:Now, your
DateTime.TryParseExactshould work as expected with the “dd/MM/yyyy” format. Here’s how you can modify your code:This should resolve the issue you’re facing.
Thanks
Jignesh KumarPosted Apr 30, 2024, 4:00 AM
Hello,
Please use this one, use TryParseExact below extension method to achieve your expected out put.