tags:

views:

102

answers:

4

Hi,

Is there a way, a good way, to test if a string than I want to transform in DateTime is dd/MM/yyyy or MM/dd/yyyy ?

Thanks,

+2  A: 

No, but you could try both when parsing:

DateTime result;
if (DateTime.TryParseExact(
    "05/10/2010",
    new[] { "MM/dd/yyyy", "dd/MM/yyyy" }, 
    CultureInfo.InvariantCulture, 
    DateTimeStyles.None, 
    out result)
)
{        
    // successfully parsed date => use the result variable
}
Darin Dimitrov
Works only for unambiguous case, otherwise the first choice will be picked. (05/10/2010 could be October 5, 2010 or May 10, 2010)
madgnome
Yes, there could be ambiguity.
Darin Dimitrov
+8  A: 

No, because it could be both. Is 11/10/2010 November 10th or October 11th?

Yes, in some cases (if one number is above 12) it will be unambiguous - but I think it's better to force one format or the other. If you just treat anything which can be done as MM/dd/yyyy that way, and move on to dd/MM/yyyy if it fails (or the other way round) then you'll get some very surprised users.

If this is part of a web application or something similar, I would try to make it completely unambiguous by using month names instead of numbers where possible.

Jon Skeet
Force ok, but for display is the first to send to database is the second
Kris-I
It's essentially an impossible problem, but most folks want to enter a simple date - 10/11/1970 - when asked for their birthday, and not be forced to spell out a month. I suppose you could assume a particular format, though geolocation or something, and then display a long-format preview of the date using that format with a client-side script.
Michael Petrotta
Assuming web, of course...
Michael Petrotta
@Kris-I: You shouldn't be formatting the `DateTime` *at all* to send it to the database. You should be sending it *as* a `DateTime` in a query parameter. That way you can't mess it up. For parsing user input, just use `DateTime.TryParseExact` specifying just the single accepted format.
Jon Skeet
A: 

Take a look at DateTime.ParseExact and DateTime.TryParseExact.

string date1 = "24/12/2010";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dt1 = new DateTime(1, 1, 1);
bool dt1Valid = false;
try
{
    dt1 = DateTime.ParseExact(date1, "dd/MM/yyyy", provider);
    dt1Valid = true;
}
catch
{
    dt1Valid = false;
}
Andrei Bularca
Why would you show code using ParseExact when you're aware of TryParseExact? You simply shouldn't use ParseExact if you're just going to catch the exception and move on.
Jon Skeet
Not to mention that this code will fail, as TryParseExact() returns boolean value...
Paweł Dyda
@Pawel Dyda: but he isn't using TryParseExact, so it will not fail.
jgauffin
@jgauffin: you haven't seen this code prior to modification :) Now it is more or less OK.
Paweł Dyda
+1  A: 

This problem will exist until all accepts and uses the ISO way. I'm a Swedish programmer working a lot with American and English clients and it's surprisingly hard to get these clients to use the standardized date format.

ISO 8601 - Use it!

Fredrik Norlin
I always try to use iso date formats where I can. No ambiguity in them. :)
Chris