C#的一些獲取時間的例子

 

從週一到週日的順序,獲取排序數值:排序

int i = DateTime.Now.DayOfWeek - DayOfWeek.Monday;
if (i == -1) i = 6;ast

獲取某日起,星期一日期:im

public static DateTime GetMondayDate(DateTime someDate)
{
int i = someDate.DayOfWeek - DayOfWeek.Monday;
if (i == -1) i = 6;// i值 > = 0 ,由於枚舉緣由,Sunday排在最前,此時Sunday-Monday=-1,必須+7=6。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Subtract(ts);
}static

獲取當前時間上週六的時間:時間

public DateTime GetLastSaturday()
{
DateTime time = DateTime.Now;
if (time.DayOfWeek == DayOfWeek.Sunday)
{
time = time.AddDays(-2);
}
else
time = time.AddDays(-1);
while (time.DayOfWeek != DayOfWeek.Saturday)
{
time = time.AddDays(-1);
}
return time;
}while

相關文章
相關標籤/搜索