Wednesday, October 3, 2007

Force English DateTime format in Web Pages.

Many people argued that i usually add very lengthy posts here in my technical blog, which they think nobody will do read them. But actually, i am targetting in my posts those who are just beginners as well. So I will try my best to minimize everything as much as possible to be OK with everyone, and everybody is welcommed to ask as many questions as he/she wish and I will be answering them as soon as possible :).


Well this problem I will be mentioning now soon is very common especially for those who live in Arabic countries, as usually they configure their windows in the Regional and language settings to Arabic - [Country].

What's The problem?

The problem is that when you get to say

DateTime.Now.ToString();
//Value -> 30/01/2006 10:33:00 م

As it's obvious, you may find instead of the AM and PM added at the end of the time, it became ص and م instead. The problem may further extend and sometimes you may find the monthe name became فبراير instead of February.
ok so what's the problem? well, if you have a field in the Database that is of DataType DateTime, and tried to insert any of the values mentioned above, you will get an error that says that the string is not a valid DateTime string.

How to Fix that?

The first thing that will come in your mind am sure, is to change the Regional and language settings in your windows configuration (Control Panel> Regional and language settings), and make everything point to United States.

This is not always the case, and wont always fix your problem. So here you are the clue. In your web Application, edit you web.config file if already available, or add it into your application, and add the following line in it.

<configuration>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="en" culture="en-US" />
</system.web>
</configuration>

This forces the website to use the English Culture and the United States Configurations, not just in time formats, but in numbers, currencies, short and long dates. And this sure solves the problem perfectly.


3 comments:

  1. Great! So the solution is to make everything American??? Not really a solution is it....

    ReplyDelete
  2. best solution thank you. But one question how can we add local time zone in web .config

    ReplyDelete