查找日历的第一天
我想要做的是创建一个简单的日历,并且我想查找特定月份的第一周的第一天。我的日历是星期一 - >星期日日历,下面的代码可以工作,但正如你所看到的那样。任何人都有更好的想法如何获得日历中的第一个日期。查找日历的第一天
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 安德烈亚斯
我只是这样做。这是很容易理解:
var firstDayOfMonth = new DateTime(year, month, 1);
DateTime startOfCalendar =
FirstDayOfWeekOnOrBefore(
firstDayOfMonth,
DayOfWeek.Monday
);
public static DateTime FirstDayOfWeekOnOrBefore(
DateTime date,
DayOfWeek dayOfWeek
) {
while(date.DayOfWeek != dayOfWeek) {
date = date.AddDays(-1);
}
return date;
}
此外,如果你想改变你的日历开始就以周一的东西,它现在是微不足道的。使用模算术的解决方案不可维护。
可以使用模计算的填料天数没有条件语句:
DateTime firstOfMonth=new DateTime(year,month,1);
var weekDay=firstOfMonth.DayOfWeek;
int fillerDays=((int)weekDay+6)%7;
DateTime firstDayInCalendar=firstOfMonth.AddDays(-fillerDays);
不错。我不知道有可能将DayOfWeek转换为int。我一直只看到它显示一天的字符串:“星期一”。 – 2012-11-10 05:25:10
@DougS它是一个枚举,您可以将任何枚举强制转换为底层整型。 – CodesInChaos 2012-11-10 08:22:20
@ CodesInChaos:很高兴知道。谢谢! – 2012-11-10 18:01:22
你可以试试这个假设第一天乌尔指的是周一
DateTime dt = new DateTime(2011, 2, 2);
Console.WriteLine(dt.AddDays((8 - (int)dt.DayOfWeek) % 7));
更新:下面的代码有一个错误!使用模算术,补偿情况,.NET在星期日开始星期!在这里查看其他解决方案(没有附加功能的解决方案)。
如果你需要找到“27”(即一个月的显示的第一天)只需使用此:
我喜欢while循环,因为它们价格昂贵,当用于不DateTime monthStart = new DateTime(year, month, 1);
DateTime monthDisplayStart = monthStart.AddDays(-((int)monthStart.DayOfWeek - 1));
LINQ
希望别人能够重复使用此代码:(如果你在美国,那么就删除[+ 6)%7)在两行)
/// <summary>
/// Expands the month.
/// | < | 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 |
/// </summary>
/// <param name="start">Some day in the month of interest, the start date is updated to become the date of firstDayInCalendar</param>
/// <returns>The number of days to show. This value is either (28, 35 or 42)</returns>
public static int ExpandMonth(ref DateTime start)
{
DateTime first = new DateTime(start.Year, start.Month, 1);
DateTime last = new DateTime(start.Year, start.Month, DateTime.DaysInMonth(start.Year, start.Month));
start = first.AddDays(-((int)first.DayOfWeek + 6) % 7);
last = last.AddDays(7 - ((int)last.DayOfWeek + 6) % 7);
return last.Subtract(start).Days;
}
//托马斯
我发现自己需要经常这样做,所以我创建了以下扩展方法。
public static DateTime FirstDateOfCalendarMonth(this DateTime dt, DayOfWeek firstDayOfWeek = DayOfWeek.Sunday)
{
dt = new DateTime(dt.Year, dt.Month, 1);
while (dt.DayOfWeek != firstDayOfWeek){
dt = dt.AddDays(-1);
}
return dt;
}
使用方法如下
var firstCalDate = DateTime.Now.FirstDateOfCalendarMonth();
它默认为星期日为第一星期几,但是你可以通过它,你喜欢的任何星期几,像这样:
var firstCalDate = DateTime.Now.FirstDateOfCalendarMonth(DayOfWeek.Monday);
+1,为ASCII艺术。 – 2011-02-03 07:36:25
+1#哇,其实我在我的学院用C语言在控制台程序中做了一个类似的日历。 – 2011-02-15 10:44:36