Change date format in whole application

1k views Asked by At

I am working on asp.net mvc2 application. Problem is that when i am displaying date in format MM/dd/yyyy application work, but i must display the date in format dd/MM/yyyy to user. I set date in this format, but then i have problem with web service because it expect date format in MM/dd/yyyy, and i always get an error.

First i thought to write simple method that would convert date from one format to another ,and display in dd/MM/yyyy . But i have to match methods that use date, and i will have to make changes at many places.

I tried to use globalization in web config file like

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

But that did not work for me. Is there some way to change date format when i display to the user, and then web service can accept it?

Thanks

1

There are 1 answers

0
Svein Fidjestøl On

In my opinion en-GB is the correct setting for culture and uiCulture since this is the culture which has the date format you want to show to the user: dd/MM/yyyy

In those places where you are calling an API which requires the MM/dd/yyyy you should specify the format explicitly as MM/dd/yyyy like this:

myDate.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

I put the CultureInfo.InvariantCulture there so that there is no chance that the serialization will be affected by future changes in the culture and uiCulture settings for the application.