5

I'm in the process of converting a couple of sites up to .net 4.0 and I seem to have a problem with regionalisaion of dates.

This code:

Date.Today.AddDays((Date.Today.DayOfWeek - 1) * -1).ToString()

In .net 3.5 produces: '19/04/2010 00:00:00' but as soon as I change the app pool to 4.0 it produces: '4/19/2010 12:00:00 AM'

Where can I change the setting that governs this?

Chris Foot
  • 343
  • 1
  • 5
  • 17
  • Just to clarify, I have already got Cultire and UI Culture set to en-GB in .Net Globalization in iis but it seems to completely ignore this setting! – Chris Foot Apr 19 '10 at 10:19
  • Could you post your web.config? – Darin Dimitrov Apr 19 '10 at 11:03
  • I have no globalization setup in the web.config at all, in fact, adding a globalization section does fix the issue for the site in question but I shouldn't have to do this for every site I want to run since 2.0 and 3.5 never had a problem! – Chris Foot Apr 19 '10 at 13:55
  • Does the OS have the correct region settings? It sounds like it's either pulling the incorrect region settings from the OS, or .NET 4 started using InvariantCulture by default. – Greg May 12 '10 at 14:05
  • From a European point of view, it's kind of arrogant by Microsoft to define Invariant culture = US cuture... – awe May 21 '10 at 13:04
  • I just found this one, too. NUnit is failing tests in a .NET 4 project because date.ToShortDateString() is producing a US date format (mm/dd/yyyy). Haven't seen that happen in .NET 3.5 SP1. Annoying! – Matt Hamilton Aug 13 '10 at 04:59

1 Answers1

2

I don't think you can try this instead though ...

Date.Today.AddDays((Date.Today.DayOfWeek - 1) * -1).ToString(dd/MM/yyy hh:mm:ss); 

That should format the date as you need it.

Microsoft basically rebuilt quitea lot from the ground up for .net 4.0 ... it's a pain in some areas like this but a real advantage in others.

I sw a guy on another forum that has a huge project consisting of thousands of lines of code that he now has to go find and replace on his datetime.tostring statements.

Best practice has always been that you should specify the format anyway otherwise you get whatever the framework decides.

Hope this helps.

War
  • 8,539
  • 4
  • 46
  • 98