3

I am wondering what is the best way to figure out how to convert a datetime? Right now my computer is setup as dd/mm/yyyy yet I am taking date as mm/dd/yyy so when I try to do

DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();

I either get nothing back or it crashes. Now I could tell it about the mm/dd/yyyy format and it probably would convert. However the problem is these dates are are coming from user uploaded files.

They could be in any format combination. I am trying to find ways to do it.

The problem I see is that I am looking at the dates in an uploaded file so I am not sure if looking say at the browser date format would help or not.

I am using asp.net mvc and I am trying to get a solution that handle date formats automatically so I don't have to ask the user about the date format they have (I figure the more things I have to ask them the less likely the user will continue on)

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 3
    So is `02/04/2012` April 2 or February 4? – dtb Feb 09 '12 at 18:17
  • chobs - you 'could' find the locale culture and work on the dates based on that!! no?? (see: http://stackoverflow.com/questions/2288383/where-is-the-system-locale-culture-set-for-net for example) – jim tollan Feb 09 '12 at 19:40

2 Answers2

1

No, you can't figure out automatically what date-time format a user meant to use once the value is on the server. You need more information to parse it correctly (e.g. 1/2/3 can mean a lot of different dates depending on the culture).

Consider one of the following solutions:

  1. Convert the entered date to a text representation in a standard format (i.e. ISO 8601 - 2012-02-09) using JavaScript on the client before you send it to the server. The code would look something like this: d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate().
  2. Send the local culture information to the server along with date value to be converted and do the conversion on the server.
  3. Force the user to enter the date in a specific format (e.g. Use 3 text boxes labeled "Month", "Day", and "Year" instead of one text box with free input).
Drew Spickes
  • 234
  • 2
  • 7
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Dang was hoping their was some super way to figure it out since it just so much better if the user does less as possible. – chobo2 Feb 09 '12 at 18:39
  • @chobo2 The user doesn't have to perform the conversion themselves. If your user is hitting your app through a web browser, just do the conversion with some JavaScript before sending it to the server. The only place you know what the user means when they enter '1/2/3' is in the browser. This is a conversion you should do before the value gets to the server. [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) is your friend when dealing with changing local DateTime representations to universal values. Alexei has laid out your only options to solve the problem. – Drew Spickes Feb 09 '12 at 22:47
1

chobo2 (I like the 'handle') :)

you can detect the locale culture and work on that at will. see the following SO Q/A for pointers:

Where is the system locale/culture set for .Net

the key is to NOT have to set anything in particular, but identify the locale and act accordingly.

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • Won't that just give me the server culture? – chobo2 Feb 09 '12 at 21:09
  • actually, yeah -correct. i previously in an 'old life' sent a javascript function from my master template to discover the locale. however, this has high dependency on javascript being enabled and thus can only partially cover your needs (tho, these days, perhaps > 75%, whereas when i took this approach several years back the assumption was 25% js coverage - therefore, was a slim option). investigate a combo of session and js functionality to close this off i feel!! – jim tollan Feb 09 '12 at 21:13