tags:

views:

2590

answers:

7

Not sure what's going on here.

I have a DateTime object, and when I try:

String.Format( "{0:dd/MM/yyyy}", _date)

the value returned is:

"24-05-1967"

What I want is

"24/05/1967"

Can anyone explain why my format string is being ignored?

A bit more background: This is a web app which started out life as .net 1.1, and I'm in the process of moving it up to 2.0/3.5.

Update:

If I change the format to {0:dd:MM:yyyy}, it returns 24:05:1967 - it's only the / in the format string that gets changed to the - char.


Resolution:

When updating the app to run under 2.0, the asp.net globalization settings were messed up.

From the web site properties, ASP.NET tab, Edit Configuration, Application Tab - the culture and UI Culture were both set to the first item in the list (af-ZA) for some bizarre reason.

+5  A: 

You likely want to use the ToString() method of your DateTime object to get the string representation you are after. A format string of "dd'/'MM'/'yyyy" will give you the format you described. Note that you need to escape literal characters in the format string using single quotes.

Example: DateTime.Now.ToString("dd'/'MM'/'yyyy");

Oppositional
Thanks, that works - I don't suppose that you know when this changed (between 1.1 and 3.5) and where it's documented?
chris
See .NET Framework 3.5: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx (.NET 3.5) and .NET Framework 1.1: http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx (.NET 1.1) for details. It does not appear the API changed for custom DateTime format strings.
Oppositional
A: 

That's odd. That formatting works correctly for me. You might want to try _date.ToString("dd/MM/yyyy") instead (though that's just a shot in the dark).

Kevin Tighe
A: 

Regional settings (on web server?) have date delimiters set to '-'. In my locale (EE) output would be "24.05.1967" :)

Arvo
+6  A: 

The / is actually the date separator for your specific culture which could be -, in other words, the format string is not ignored but actually used correctly. Look at what CultureInfo is associated with the running thread:

System.Threading.Thread.CurrentThread.CurrentCulture

If you try this:

String.Format(new CultureInfo("en-US"), "{0:dd/MM/yyyy}", DateTime.Now);

You will get the date formatted with / since that is the correct separator for en-US. What you probably should do is use the short date format string and make sure that the thread has the appropriate culture associated, then this would get you what you want and it will work in a globalized application as well:

DateTime.Now.ToString("d", Thread.CurrentThread.CurrentCulture);

Hope this helps!

Carl Serrander
+1 for remembering that '/' is converted for culture AND pointing out the 'd' short date string specifier may be better :)
Nij
A: 

Unless you specify the IFormatProvider (usually a CultureInfo object), it will by default use Thread.CurrentThread.CurrentCulture's date/time formatting, which will give you the changing results.

And yes, it will change / but not :.

The solution in this case is to specify the format provider, like this:

String.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", _date)
Lasse V. Karlsen
+1  A: 

The "/" character is the culture neutral placeholder for the date time separator character which is defined in Regional Settings. So is the ":" character for time.

To embed the "/" literally and not as a placeholder you need to enclose it in single quotation marks '/' in the format string.

Pop Catalin
A: 

String.Format( "{0:dd/MM/yyyy}", Convert.ToDateTime(_date));

this will give you your required output

vijay