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

Jignesh KumarRank #91 · 22.9K reputationPosted Apr 30, 2024, 12:00 AM EDT
Hello,
Please use this one, use TryParseExact below extension method to achieve your expected out put.
DateTime StartDate; DateTime EndDate; string sDate = TrinData[4].ToString(); // Assuming TrinData[4] contains the start date string eDate = TrinData[5].ToString(); // Assuming TrinData[5] contains the end date string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "dd/MM/yyyy" }; // Parse start date if (!DateTime.TryParseExact(sDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out StartDate)) { lblMessage.Text += "The start date is not in the correct format."; } // Parse end date if (!DateTime.TryParseExact(eDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out EndDate)) { lblMessage.Text += "The end date is not in the correct format."; } // Check if end date is greater than or equal to start date if (StartDate > EndDate) { lblMessage.Text += "The Ending Date must be greater or equal to the starting date."; }Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 30, 2024, 6:18 AM EDT
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:CultureInfo customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; customCulture.DateTimeFormat.DateSeparator = "/"; Thread.CurrentThread.CurrentCulture = customCulture;Now, your
DateTime.TryParseExactshould work as expected with the “dd/MM/yyyy” format. Here’s how you can modify your code: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 = { "dd/MM/yyyy" }; CultureInfo customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; customCulture.DateTimeFormat.DateSeparator = "/"; Thread.CurrentThread.CurrentCulture = customCulture; if (!DateTime.TryParseExact(sDate, formats, customCulture, DateTimeStyles.None, out StartDate)) { Console.Write("The DateTime is in another format."); } if (!DateTime.TryParseExact(eDate, formats, customCulture, DateTimeStyles.None, out EndDate)) { Console.Write("The DateTime is in another format."); } if (StartDate.Date > EndDate.Date) { lblMessage.Text += "The Ending Date must be greater or equal to starting date "; }This should resolve the issue you’re facing.
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 30, 2024, 7:18 AM EDT
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:string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "dd/MM/yyyy" };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.Prasad RaveendranRank #320 · 7.8K reputationPosted Apr 30, 2024, 9:09 PM EDT
try the below code
DateTime startDate, endDate; string[] dateFormats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy" }; bool TryParseDate(string dateString, out DateTime date) { foreach (string format in dateFormats) { if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { return true; } } return false; } if (!TryParseDate(TrinData[4].ToString(), out startDate) || !TryParseDate(TrinData[5].ToString(), out endDate)) { Console.Write("Invalid date format."); } else if (startDate > endDate) { lblMessage.Text += "The ending date must be greater than or equal to the starting date."; }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.