得到任意一個時間是當前所在星期得星期幾。
代碼 public static void GetChineseDayOfWeek() { DateTime curDatetime = DateTime.Now; Console.WriteLine("當前日期是:" + curDatetime.ToString("yyyy-MM-dd HH:mm:ss")); DayOfWeek dayOfWeek = curDatetime.DayOfWeek; Console.WriteLine("當前星期枚舉值:" + dayOfWeek); string retValue = ""; switch (dayOfWeek) { case DayOfWeek.Monday: retValue = "星期一"; break; case DayOfWeek.Tuesday: retValue = "星期二"; break; case DayOfWeek.Wednesday: retValue = "星期三"; break; case DayOfWeek.Thursday: retValue = "星期四"; break; case DayOfWeek.Friday: retValue = "星期五"; break; case DayOfWeek.Saturday: retValue = "星期六"; break; case DayOfWeek.Sunday: retValue = "星期日"; break; } Console.WriteLine("當前星期得中文名:" + retValue); }
代碼說明
DayOfWeek 是一個枚舉型,如果轉成中文得話,使用switch進行轉換。