0

Frustrating problem with DateTime, we have a section with a date picker on and if the culture is set to en-GB and the date is 01/11/2012 (dd/mm/yyyy) and then the culture is swapped to Chinese (zh-CN) the date is passed in the old culture format and when it is converted to a DateTime the datetime becomes 2012/1/11 when the culture should be (yyyy/mm/dd).

This seems to be the only culture that I have come across where the conversion is going wrong as it should be 2012/11/1 as 11 is the month.

Does anyone have any ideas why it isn't converting correctly?

Sorry guys this is my fault for not being clear enough, more detail needed.

We have a reporting form that allows users to select a date range, on that form we use use a date range picker (http://www.filamentgroup.com/lab/update_date_range_picker_with_jquery_ui/) to populate a readonly textbox that is submitted to filter the results. Users can select from a dropdown their current culture (needed for our users as they wanted to easily swap between English and Chinese without changing browser settings, If the culture is en-GB then the date range string may read "01/01/2012---01/11/2012" which is 1st January 2012 - 1st November 2012. This is stored in the url, e.g: &DateRange=01%2F01%2F2012+---+01%2F11%2F2012.

Now if the user swaps to Chinese it calls the current page with all the same query parameters but also with the culture parameter changed (we allow the culture to be overridden by an URL parameter) which means the dateformat 01/11/2012 is in the query string, when we pass this using:

DateTime.TryParse(endDateString, out endDate);

the DateTime object contains a date of 11th January 2012 instead of 1st November 2012.

Is there a way I could store the culture info that the date string is in and use that to convert to the new culture info if the culture is swapped?

Any better ideas would be greatly appreciated, swapping from en-GB to en-IN (India) seems to work fine as does swapping to es-MX (Mexico). It just seems to be chinese but that could be pot luck based on these languages date formats.

Many thanks for taking the time to read this.

user351711
  • 3,171
  • 5
  • 39
  • 74
  • Show how you changes the cultures and how you pass the value. – H H Nov 01 '12 at 13:16
  • This is a common problem when you store dates in strings. Which is why DateTimePicker doesn't use strings. And why you should not use strings in your own code either. – Hans Passant Nov 01 '12 at 13:36
  • I don't store dates in strings but the date string has to get from the client to server and be parsed into a DateTime object. – user351711 Nov 01 '12 at 13:50

2 Answers2

0

Use strings to move datetime around in such cases, if you are not processing datetime and just storing it.After that you can change the format before storing it in the database.

SutharMonil
  • 78
  • 3
  • 19
0
DateTime.TryParse(endDateString, out endDate);

is using whatever culture is default on the server.

The basic solution would be:

var ci = GetCultureInfoFromRequest();
DateTime.TryParse(endDateString, out endDate, ci);
H H
  • 263,252
  • 30
  • 330
  • 514
  • I went with something like this, unfortunately we'll have to store the culture the current date was formatted in and then use that if they swap. Thanks for everyone's suggestions. – user351711 Nov 01 '12 at 19:51