C#的一些获取时间的例子
从周一到周日的顺序,获取排序数值:
int i = datetime.now.dayofweek - dayofweek.monday;
if (i == -1) i = 6;
获取某日起,星期一日期:
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);
}
获取当前时间上周六的时间:
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;
}