datetime.tryparse("datetime in dd/MM/yyyy format",datetimeobject) returns false for dd/mm/yyyy
-
I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.
DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.
Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats
-
I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.
DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.
Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats
I am unable to replicate the issue at my end. The sample code I used to test it (form contains a textbox for input, a button with the test code below, and a label to display the result):
DateTime getDate;
bool valid = DateTime.TryParse(textBox1.Text, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out getDate);if (valid)
label1.Text = getDate.ToLongDateString();
else
label1.Text = "Not a valid date";I can enter various formats, using either a number for the month or the short-hand notation or the long notation and it all works fine. Could you tell me exactly what input doesn't work properly, for instance when you try this code?
-
I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.
DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.
Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats
schampacc wrote:
I have requirement where user can pass all types of datetime formats.
Forget about the code for a moment and think about this. What is meant by 1) 04/05/2011 2) 05/04/2011 Both could mean either 4 May 2011 or 5 April 2011 and it's likely that even the Startrek Universal Translator cannot solve this fundamental problem. Alan.
-
schampacc wrote:
I have requirement where user can pass all types of datetime formats.
Forget about the code for a moment and think about this. What is meant by 1) 04/05/2011 2) 05/04/2011 Both could mean either 4 May 2011 or 5 April 2011 and it's likely that even the Startrek Universal Translator cannot solve this fundamental problem. Alan.
-
And does it return true for both 04/05/2011 and 04/25/2011? How the date/time is interpreted depends on the locale used. This is good, cf. Star Trek Universal Translator in previous post.
-
And does it return true for both 04/05/2011 and 04/25/2011? How the date/time is interpreted depends on the locale used. This is good, cf. Star Trek Universal Translator in previous post.
-
schampacc wrote:
It returns true for 05/04/2011 but returns false for 25/04/2011
With that information I can deduce that your system's Culture uses MM/dd/yyyy format and it shouldn't be a surprise that TryParse rejects a month number of 25. When the day number is less than 13 it is not possible to understand a date without prior knowledge of the Culture. Therefore I can guess that "25/04/2011" is 25 April 2011 but I can't say whether "05/04/2011" is 5 April or 4 May unless I know the field order within the string. Alan.
-
I have requirement where user can pass all types of datetime formats. Also I need to validate all the datetime formats entered by user.
DateTime.TryParse() returns false, if the date passed is in dd/MM/yyyy format. I have noticed in all the forums this issue is been noted.
Can anyone reply why this error is been thrown and whats the solution so as to pass all datetime formats
schampacc wrote:
pass all types of datetime formats
That isn't possible. Different cultures expect different forms of dates and those forms are not deterministically unique without additional information. So either you must limit the possible forms or you must provide a way for the user (or user app) to tell you what form is expected. There is no other possibility.
-
schampacc wrote:
pass all types of datetime formats
That isn't possible. Different cultures expect different forms of dates and those forms are not deterministically unique without additional information. So either you must limit the possible forms or you must provide a way for the user (or user app) to tell you what form is expected. There is no other possibility.
Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.
DateTimeStyles styles = DateTimeStyles.None;
DateTime startDate;
foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
DateTime.TryParse("stringDate", cInfo, styles, out startDate)
} -
Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.
DateTimeStyles styles = DateTimeStyles.None;
DateTime startDate;
foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
DateTime.TryParse("stringDate", cInfo, styles, out startDate)
}Will you still not have problems when the day number is less than 13? 04/05/11 and 05/04/11 will probably be ok with several cultures?
//daniel
modified on Tuesday, September 13, 2011 5:06 AM
-
Will you still not have problems when the day number is less than 13? 04/05/11 and 05/04/11 will probably be ok with several cultures?
//daniel
modified on Tuesday, September 13, 2011 5:06 AM
-
Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.
DateTimeStyles styles = DateTimeStyles.None;
DateTime startDate;
foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
DateTime.TryParse("stringDate", cInfo, styles, out startDate)
}This is not the way to solve this - in fact, it's an incredibly naive solution. As an example, 9/11/2001 - what happened on that day? It was either a terrible disaster in America or a very quiet day in November. You should use the universal datetime format in your application, which is agnostic of ALL cultures.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
This is not the way to solve this - in fact, it's an incredibly naive solution. As an example, 9/11/2001 - what happened on that day? It was either a terrible disaster in America or a very quiet day in November. You should use the universal datetime format in your application, which is agnostic of ALL cultures.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Thats right. Then in this case one should go for one particular format only. The issue was to validate the date is in valid dateformat or not. It can be in any valid dateformat. This issue can be solved using the above loop.
schampacc wrote:
Then in this case one should go for one particular format only.
And your test still doesn't hold up. Take a look at 4/20/2011. Is that a valid date of the form April 20th or an invalid date when someone tried to enter the 4th day of the 20th month?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I mean that the date should conform to ISO 8601.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi All, I have found the solution to support all datetime formats. First I need to inform is that datetime objects uses the system culture by default. But we have a solution for this. You can loop in all the cultures available and check if the passed datetime is in valid format or not by using the below code.
DateTimeStyles styles = DateTimeStyles.None;
DateTime startDate;
foreach (CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
DateTime.TryParse("stringDate", cInfo, styles, out startDate)
}schampacc wrote:
I have found the solution to support all datetime formats.
No you haven't. I said it was impossible - which it is. There is no solution. So whatever you are doing is certainly not a solution. What you are probably doing is testing within a LIMITED cultural scope. Which might be what you need to do but isn't what you asked. And if it isn't what you need to do then your solution will fail.
-
I mean that the date should conform to ISO 8601.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Pete O'Hanlon wrote:
conform to ISO 8601
Hear hear! I'm glad I didn't have to say it this time. :thumbsup:
-
Pete O'Hanlon wrote:
conform to ISO 8601
Hear hear! I'm glad I didn't have to say it this time. :thumbsup:
:laugh:
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)