找到日历的第一天

找到日历的第一天

本文介绍了找到日历的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是创建一个简单的日历,并且我想查找特定月份第一周的第一天。我的日历是星期一 - >星期日日历,下面的代码工作,但正如你可以看到它不是那么好。任何人都对如何获得日历中的第一个日期有更好的想法。

  var now = new DateTime(Year,Month, 1); 
now = now.AddDays(1-(int)now.DayOfWeek);
now = now.Day> 15? now:now.AddDays(-7);

日历将最终如下所示:

  | < | 2011年1月| > | 
------------------------------------
| Mo | Tu |我们| 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 |

在这个图像这是我试图找到的日期。 / p>

解决方案(找到更好的/更清洁的循环然后计算)

  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);
}
返回日期;
}

public DateTime LastDay()
{
var date = new DateTime(Date.Year,Date.Month,
DateTime.DaysInMonth .Year,Date.Month));
while(true)
{
if(date.DayOfWeek == DayOfWeek.Sunday)
return date;
date = date.AddDays(1);
}
返回日期;
}

/ BR
Andreas

解决方案

我只是这样做。它很容易理解:

  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);
}
返回日期;
}

此外,如果您想更改日历, ,它现在是微不足道的。使用模运算的解决方案将不是可维护的。


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

/BRAndreas

解决方案

I would just do this. It is so easy to understand:

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

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.

这篇关于找到日历的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:26