查找日历的第一天
我想做的是创建一个简单的日历,我想找到特定月份第一周的第一天。我的日历是星期一 ->星期日日历和以下代码可以工作,但正如您所看到的,它并不是那么好。任何人都对如何获取日历中的第一个日期有更好的想法。
var now = new DateTime(Year, Month, 1);
now = now.AddDays(1-(int)now.DayOfWeek);
now = now.Day > 15 ? now : now.AddDays(-7);
日历最终看起来像这样:
| < | Jan 2011 | > |
------------------------------------
| Mo | Tu | We | Th | Fr | Sa | Su |
|[27]| 28 | 29 | 30 | 31 | 01 | 02 |
| 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| .. | .. | .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. | .. | .. |
| 31 | 01 | 02 | 03 | 04 | 05 | 06 |
在这个“图像”中,它是我要查找的 [27] 日期。
解决方案(发现我更好/更干净地循环然后计算):
public DateTime FirstDay()
{
var date = new DateTime(Date.Year, Date.Month, 1);
while (true)
{
if (date.DayOfWeek == DayOfWeek.Monday)
return date;
date = date.AddDays(-1);
}
return date;
}
public DateTime LastDay()
{
var date = new DateTime(Date.Year, Date.Month,
DateTime.DaysInMonth(Date.Year, Date.Month));
while (true)
{
if (date.DayOfWeek == DayOfWeek.Sunday)
return date;
date = date.AddDays(1);
}
return date;
}
/BR 安德烈亚斯
What i want to do is to create a simple calendar, and I want to find the first day of the first week of a specific month. My calendar is a Monday -> Sunday calendar and the following code works, but as you can see it's not that nice. Anyone have any better idea on how to get the first date in the calendar.
var now = new DateTime(Year, Month, 1);
now = now.AddDays(1-(int)now.DayOfWeek);
now = now.Day > 15 ? now : now.AddDays(-7);
The calendar will end up looking like this:
| < | Jan 2011 | > |
------------------------------------
| Mo | Tu | We | Th | Fr | Sa | Su |
|[27]| 28 | 29 | 30 | 31 | 01 | 02 |
| 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| .. | .. | .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. | .. | .. |
| 31 | 01 | 02 | 03 | 04 | 05 | 06 |
And in this "image" it's the [27] date that i'm trying to find.
Solution (Found i better/cleaner to loop then calculate):
public DateTime FirstDay()
{
var date = new DateTime(Date.Year, Date.Month, 1);
while (true)
{
if (date.DayOfWeek == DayOfWeek.Monday)
return date;
date = date.AddDays(-1);
}
return date;
}
public DateTime LastDay()
{
var date = new DateTime(Date.Year, Date.Month,
DateTime.DaysInMonth(Date.Year, Date.Month));
while (true)
{
if (date.DayOfWeek == DayOfWeek.Sunday)
return date;
date = date.AddDays(1);
}
return date;
}
/BR
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我就这么做。它非常容易理解:
此外,如果您想更改日历以从星期一以外的时间开始,现在很简单。使用模算术的解决方案不太可维护。
I would just do this. It is so easy to understand:
Additionally, if you want to change your calendar to start on something other than Monday, it's trivial now. A solution using modulo arithmetic would not be as maintainable.
您可以使用模来计算填充天数,而无需条件语句:
You can use modulo to calculate the number of filler days without a conditional statement:
你可以尝试这个假设你指的第一天是星期一
You can try this assuming first day ur referring to is Monday
我不喜欢 while 循环,因为与 LINQ 一起使用时它们的成本很高
希望其他人可以重用此代码:(如果您在美国,则只需删除两行中的 [ + 6) % 7)] )
//Thomas
I do not like while loops, because they are expensive when used with LINQ
Hope someone else can reuse this code: (if you are in the USA, then just remove [ + 6) % 7)] in two lines)
//Thomas
我发现自己经常需要这样做,所以我创建了以下扩展方法。
像这样使用
它默认将星期日作为第一个 DayOfWeek,但您可以将其传递给您喜欢的任何 DayOfWeek,如下所示:
I found myself needing to do this quite often, so I created the following extension method.
Use it like this
It defaults to Sunday as the first DayOfWeek, but you can pass it whatever DayOfWeek you like, like this:
更新:以下代码有错误!使用模运算,为了补偿这种情况,.NET 从周日开始一周!请参阅此处的其他解决方案(没有具有额外功能的解决方案)。
如果您需要查找“27”(即显示月份的第一天),只需使用以下命令:
UPDATE: Following code has a bug! Use modulo arithmetic, to compensate the circumstance, .NET begins the week on sunday! See other solutions here (without the one with an extra function).
If you need to find the "27" (i.e. the first day for the display of a month) simply use this: