C#中获取日期所对应的星期。下面的函数就是用来处理传递过来的日期所对应的星期几。这在C#中相对较容易些,因为C#的DateTime类中有一个属性DayOfWeek专门用来获取某个日期所对应的星期的,只要将其转换成相应的数字就可以了,具体源代码如下
/// <summary>
/// 获取日期对应的星期
/// </summary>
/// <param name="dateTime">要判定的人日期</param>
/// <returns>返回日期所对应的星期</returns>
public static int GetWeekIntDay(DateTime dateTime)
{
switch (dateTime.DayOfWeek)
{
case DayOfWeek.Monday: return 1;
case DayOfWeek.Tuesday: return 2;
case DayOfWeek.Wednesday: return 3;
case DayOfWeek.Thursday: return 4;
case DayOfWeek.Friday: return 5;
case DayOfWeek.Saturday: return 6;
case DayOfWeek.Sunday: return 7;
default: return 0;
}
}
新闻热点
疑难解答