2

I've a datetime:

DateTime dt = new DateTime(2003, 5, 1);
dt.DayOfWeek // returns Thursday

How I can split only first three characters from DayOfWeek e.g. Thu?

stefan
  • 4,906
  • 12
  • 47
  • 82

2 Answers2

9

If you mean for it to work in different cultures, then your best bet is probably:

var abbr = culture.DateTimeFormat.GetAbbreviatedDayName(dayOfWeek);

where culture is a CultureInfo, for example:

var culture = CultureInfo.CurrentCulture;

In English cultures, this is the 3-letter version:

Sun
Mon
Tue
Wed
Thu
Fri
Sat
Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
2

Marcs approach is the best here. But if i understand your question in a more general way, how to get the first three letters of an enum-value, you can use string methods if you use enum.ToString:

DateTime dt = new DateTime(2003, 5, 1);
string dow = dt.DayOfWeek.ToString();
dow = dow.Length > 3 ? dow.Remove(3) : dow;
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891