C#中获取任意日期为星期几三种方法分享


摘要:
下文分享使用c#代码获取任意日期所属星期数的方法分享,如下所示:
实验环境:windows操作系统



方法1:使用dayofweek获取当前星期数,再进行相关转换

string[] DayWeekArray = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
string t_ = DayWeekArray[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString(); 
labelWeekDayInfo.Text = t_;  //星期数赋值


方法2:使用CultureInfo语言信息直接对星期数进行转换

labelWeekDayInfo.Text  =
 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);


方法3:使用switch case 判断生成星期数

 var t_="";
 var tWeek = DateTime.Today.DayOfWeek.ToString(); 
switch (tWeek) 
{ 
case "Monday": 
t_ = "星期一"; 
break; 
case "Tuesday": 
t_ = "星期二"; 
break; 
case "Wednesday": 
t_ = "星期三"; 
break; 
case "Thursday": 
t_ = "星期四"; 
break; 
case "Friday": 
t_ = "星期五"; 
break; 
case "Saturday": 
t_ = "星期六"; 
break; 
case "Sunday": 
t_ = "星期日"; 
break; 
} 
labelWeekDayInfo.Text= t_;