获取当前星期几的名单从DateTime.Now [英] Getting the list of days of the current week from DateTime.Now

查看:640
本文介绍了获取当前星期几的名单从DateTime.Now的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的矛盾或者琐碎问题是创造我的天格式DD列表 - YY从今天的日期 - MM。假设我们有今天是2015年11月4日确定?我有兴趣创建一个正好开始从星期一,02.11.2015至周日,2015年8月11日的日期时间的列表。这怎么可能?我想最初:

My paradoxical or perhaps trivial problem is to create me a list of the days in format DD - MM - YY from the date of today . Suppose we have today is the " 11/04/2015 " ok ? I would be interested to create a list of datetime that starts exactly from Monday, 02.11.2015 to Sunday, 08.11.2015 . How is this possible? I thought initially :

DateTime today = new DateTime.Now;

int posDayOfWeek = (int)today.DayOfWeek;

if (posDayOfWeek < 7 && posDayOfWeek > 1)
{
    // And from there a black hole in my brain ....
}

我真的不知道该怎么办......

I do not really know how to do ....

感谢您
亲切
克里斯蒂安Capannini

Thank you cordially Cristian Capannini

推荐答案

假设你总是希望周一至周日,你只需要像:

Assuming you always want Monday to Sunday, you just need something like:

DateTime today = DateTime.Today;
int currentDayOfWeek = (int) today.DayOfWeek;
DateTime sunday = today.AddDays(-currentDayOfWeek);
DateTime monday = sunday.AddDays(1);
// If we started on Sunday, we should actually have gone *back*
// 6 days instead of forward 1...
if (currentDayOfWeek == 0)
{
    monday = monday.AddDays(-7);
}
var dates = Enumerable.Range(0, 7).Select(days => monday.AddDays(days)).ToList();

这篇关于获取当前星期几的名单从DateTime.Now的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆