欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#的一些获取时间的例子

程序员文章站 2024-01-03 08:23:52
从周一到周日的顺序,获取排序数值: int i = DateTime.Now.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6; 获取某日起,星期一日期: public static DateTime GetMondayDate(DateTime so ......

 

从周一到周日的顺序,获取排序数值:

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;
}

 

上一篇:

下一篇: