Date Sync on Localized Systems

We have a web application that generates reports. Data is taken from the database.

When we launched the web application on a localized system, it explodes. We traced the problem to DateTime.Parse (dateString); call me.

The dates stored in the database are somewhat dependent on the locale of the machine.

In the English system, the date is saved as MM / DD / YYYY (06/25/2009), which is completely normal.

In the Russian system, the date is stored as MM.DD.YYYY (06.25.2009). This is strange, because the default value (I checked) for the short date format in Russian systems is dd.MM.yyyyy ... It should be like this 25.06.2009. I do not understand why it accepted the default separator (.), But not the default date format.

Anyway, how can I parse a date string in a localized system? If I use Russian cultureinfo, it still throws an error, since it expects dd.MM.yyyyy.

Thanks!

+4
source share
5 answers

You should never store a localized date string in a database.

You must save the dates in the date columns and then localize the data when it is displayed.

1. Set the locale of your UI site

In this example, set lang as requested, but may have a different mechanism.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim lang As String If HttpContext.Current.Request.Path.Contains("/en/") Then lang = "en" 'english' ElseIf HttpContext.Current.Request.Path.Contains("/pt/") Then lang = "pt" 'portugues' Else lang = "es" 'español, the default for the site' End If Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang) Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang) End Sub 

2. Get input variables up to date

Retrieving data from text input is simple, this is an example, but you should be fine with checking the date.

 dim theDate as date if IsDate( txtDate.text ) then theDate = DateTime.Parse(txtDate.text) else 'throw exception or something end if 

Edit

Since you say you cannot change the way it is stored, you have big problems if you don't have a db entry to tell the date format. The problem is not separators, but when you find a date like 6/3/2009, you don’t know if it is March 6th or June 3rd.

+6
source

In a database, if you must store it as text, you must standardize the format. However, it is better to save it as a datetime , and there is nothing to analyze - just read it from the database as a date ...

If you have a string, use a known culture (possibly an invariant culture) when parsing:

 DateTime when = DateTime.Parse(s, CultureInfo.InvariantCulture); 
+2
source

To be dumb, you need:

  • Know the expected format in the database. If at all possible, make it either a single standard format, or a string, or preferably a date / time type in the database (which can then be read from the database as DateTime , without code that performs any conversion).
  • Use the expected format by calling DateTime.ParseExact and explicitly specifying a format string.
+1
source

It really depends on the DBMS. See if you can get a database for storing dates in an invariant culture or a specific (immutable) culture that you select, and then set the correct CultureInfo to the DateTime.Parse() method. It doesn’t seem like this is an option since you have no control over the storage of dates.

You will need to find out if you can understand how it stores dates and act accordingly. Check the OS settings in the Russian system; maybe someone changed it by default and the database responds accordingly? If so, you can probably react in the same way.

Otherwise, you will need some correlation between the system and the way dates are stored. If all Russian systems store it in the same way, you can enable the current culture and use ParseExact . This seems like a little hack, but I'm not sure if you have any other choice.

0
source

It looks like your problem is with your database.

In the windows you should: - check the locale setting of your data source, - your data connection string, - or change the default language for the user to the RDBMS.

Each of them can guarantee that your data will be returned in the correct format.

The exact details of the solution depend, of course, on the database you are talking to and the operating system you are using.

0
source

All Articles