Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ToString formatting

I'm displaying localized short dates by providing culture info to DateTime.ToString method. By now I was using

x.ToString("d", ci); // 23.12.2000

that displays short date. But now I would like to also include abbreviated day name. I tried

x.ToString("ddd d", ci); // pon 23

but now d becomes day specifier instead of short date format so instead of day name and short date I only get day name and day number.

How do I convince formatter to display day along with predefined culture short date format?

like image 533
Robert Koritnik Avatar asked Mar 28 '11 10:03

Robert Koritnik


2 Answers

How about:

string.Format(ci, "{0:ddd} {0:d}", x)
like image 179
Kristoffer Lindvall Avatar answered Oct 06 '22 23:10

Kristoffer Lindvall


The "standard" formatting strings work by obtaining the equivalent property of the CultureInfo's DateTimeFormat. In this case "d" finds the ShortDatePattern property, which will be something like "dd.MM.yyyy", "dd/MM/yyyy", "MM/dd/yyyy", "yyyy-MM-dd" and so on, depending on that locale in question.

Hence you can make use of it in a custom pattern like so:

x.ToString("ddd " + ci.DateTimeFormat.ShortDatePattern, ci) // Sat 2000-12-23 on my set up, should presumably be pon 23.12.2000 on yours
like image 33
Jon Hanna Avatar answered Oct 07 '22 01:10

Jon Hanna