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
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)