views:

955

answers:

8

How can I achieve the following with a format string: Do 01.01.2009 ? It has to work in all languages (the example would be for Germany). So there should only be the short weekday and then the short date.

I tried 'ddd d' (without the '). However, this leads to 'Do 01'. Is there maybe a character I can put before the 'd' so that it is tread on its own or something like that?

+3  A: 
DateTime.Now.ToString("ddd dd/MM/yyyy")
Darin Dimitrov
A: 

Hi darin,

does this use '/' instead of '.' if I'm in the english domain?

No, you can use '/' instead of '.' to represent the date separator as defined in the current DateTimeFormatInfo.DateSeparator. I have edited my post accordingly.
Darin Dimitrov
I need it to be language independend. So a German user should get '.' and an English user '/'. Hope it's more clear now.
That is exactly what you will get with "ddd dd/MM/yyyy"
Darin Dimitrov
You are totaly right. Thanks a lot!
To verify this you can try: DateTime.Now.ToString("ddd dd/MM/yyyy", CultureInfo.CreateSpecificCulture("de-DE") vs DateTime.Now.ToString("ddd dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")
Darin Dimitrov
Thanks for the tip. I'll try that.
A: 

You should be using the ISO 8601 standard if you are targeting audiences with varied spoken languages.

DateTime.Now.ToString("ddd yyyy-MM-dd");

Alternatively, you can target the current culture with a short date:

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

or a long date:

DateTime.Now.ToString("D", Thread.CurrentThread.CurrentCulture);
cowgod
If you can only use one format, then ISO 8601 is good. Better, though, is to adapt the format to the client's preferred notation.
Jonathan Leffler
A: 

To get the locale specific short date, as well as the locale day name then you're going to have to use two calls, so:

 myDate.ToString("ddd ") + myDate.ToString("d");

Have you considered using the long date format instead?

Rowland Shaw
A: 

If you want to localize (I assume so, since you said "all languages"), you can use CultureInfo to set the different cultures you want to display. The MSDN library has info on Standard Date and Time Format Strings and CultureInfo Class.

The example MSDN provides:

// Display using pt-BR culture's short date format
DateTime thisDate = new DateTime(2008, 3, 15);
CultureInfo culture = new CultureInfo("pt-BR");      
Console.WriteLine(thisDate.ToString("d", culture));  // Displays 15/3/2008
Jared Harley
A: 

Yes but the long date format is well, to long ;)

The Problem is I can't use calls at all because I use it in a database / datetime picker environment and can only use one format string. There should be no code involved so this is I reason I'm posting here ;)

A: 

Just for reference, in Java it goes like this:

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
String formattedDate = dateFormat.format(date);
Yuval A
A: 

If you want to ensure the same characters are used as separators, you have to use a backslash to escape the character, otherwise it will default to the locale you are in. I recommend using this string if you want the format you specified in your question

DateTime.Now.ToString("ddd dd.MM.yyyy");

To use forward slashes instead, you should escape them so that they always output as slashes.

DateTime.Now.ToString("ddd dd\\/MM\\/yyyy");
Dan Herbert