要获取当前日期是星期几可以使用DateTime下的DayOfWeek方法。
首先我们在脚本中添加System引用:
using System;
然后我们再调用DateTime下的DayOfWeek方法:
DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek; //获取当前是星期几
由于返回的是英文,所以我们还要转化为中文,可以通过Switch语句转化:
public string GetChineseDayOfWeek(DayOfWeek dayOfWeek)
{
switch (dayOfWeek)
{
case DayOfWeek.Sunday:
return "星期日";
case DayOfWeek.Monday:
return "星期一";
case DayOfWeek.Tuesday:
return "星期二";
case DayOfWeek.Wednesday:
return "星期三";
case DayOfWeek.Thursday:
return "星期四";
case DayOfWeek.Friday:
return "星期五";
case DayOfWeek.Saturday:
return "星期六";
default:
return "";
}
}
最后调用转化方法:
string chineseDayOfWeek = GetChineseDayOfWeek(dayOfWeek);
Debug.Log("今天是" + chineseDayOfWeek);
打印结果:
这就简简单单获得了当前日期是星期几,我们就可以根据需要使用它了。?