Andrew Skirrow
Andrew Skirrow

Reputation: 3451

ASP.NET Date Format

On every environment except the problem environment

The server correctly recognizes that all clients are in the UK and parses UK formatted dates:

DD/MM/YYYY

On the problem server

The problem only appears to happen on one server. This server seems to incorrectly require american dates:

MM/DD/YYYY

What I've tried already

What I cannot try

I'm using MS ASP.NET MVC which is automatically de-serializing the HTML form data into an object, so I can't manually specify the date format -- e.g. using DateTime.Parse(myDateStr, "dd/MM/yyyy") as the parsing is done by Microsoft MVC.

Is there anything else I can do. This is an intranet app, and all clients are in the UK.

Upvotes: 2

Views: 2384

Answers (5)

Shakeel
Shakeel

Reputation: 26

You needs to do following two things.

  1. Set Culture in web.config as given below

    uiCulture="en-GB" culture="en-GB" .

  2. Set your form method to POST as I have mentioned below

    using (Html.BeginForm("DailyReport", "Reports", FormMethod.Post, new { id = "ActivityReport" }))
    

DailyReport is your controller action method, Reports is controller and most important thing is FormMethod.Post by setting form method to POST will solve your Date format problem.

Enjoy!

Upvotes: 1

Sasha
Sasha

Reputation: 8860

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

Upvotes: -1

Ron Sijm
Ron Sijm

Reputation: 8758

Your server probably runs as a different culture. If you only run one project on your server, you can change its culture like Eoin Campbell just suggested

but if your server runs other projects that are culture aware, you can just force the thread of your project into UK mode like so:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB")
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB")

Without affecting all other things running on the server

Upvotes: 0

Jim
Jim

Reputation: 1735

One possible cause is the system setting of date/time format is set to US. You can check that in Region and Language in Control Panel.

enter image description here

Upvotes: 1

Eoin Campbell
Eoin Campbell

Reputation: 44308

If you're overriding the current culture like that in your globalization section, make sure you specify the UICulture & Culture options.

However given that you've got this problem on only 1 out of many servers, I would expect that the server itself is configured differently. If you can log in to your problem-server as an admin, check the following (this is windows 7 but should be similar on Windows Server 2008)

Control Panel > Region & Language > Formats Tab > Format: "English (United Kingdom)"

and verify that the dates are expected in dd/MM/yyyy format

Upvotes: 5

Related Questions