2010-04-26 47 views
5

的我,从一个日期范围用途的功能如何获得上周五使用一个月(县).NET

public static List<DateTime> GetDates(DateTime startDate, int weeks) 
{ 
    int days = weeks * 7; 

    //Get the whole date range 
    List<DateTime> dtFulldateRange = Enumerable.Range(-days, days).Select(i => startDate.AddDays(i)).ToList(); 

    //Get only the fridays from the date range 
    List<DateTime> dtOnlyFridays = (from dtFridays in dtFulldateRange 
            where dtFridays.DayOfWeek == DayOfWeek.Friday 
            select dtFridays).ToList(); 
    return dtOnlyFridays; 
} 

的返回我只有周五的函数:“从日期列表直到StartDate指定的星期数,即如果startdate是2010年4月23日,星期数是1,那么程序应该返回从2010年4月16日到开始日期的日期“。

我打电话的功能:

DateTime StartDate1 = DateTime.ParseExact("20100430", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); 
List<DateTime> dtList = Utility.GetDates(StartDate1, 4).ToList(); 

现在的要求已经改变了一点。我只需要找出每个月的最后一个星期五。 该函数的输入将保持不变。

+0

@Newbie:C#的版本很重要,但也是.NET的版本,因为C#本身无法帮助您 - 您需要.NET Framework。 – 2010-04-26 05:59:52

+0

这是你的功课吗? – 2010-04-26 06:41:13

+0

更习惯的实现将使用'IEnumerable '而不是将所有内容都转换为'List '。 – 2010-04-26 06:42:36

回答

6

您已经拥有指定范围内的星期五列表。现在再次像这样查询:

List<DateTime> lastFridays = (from day in fridays 
           where day.AddDays(7).Month != day.Month 
           select day).ToList<DateTime>(); 

希望这会有所帮助。

1

查看下个月的第一天是星期几,然后减去足够的天数以获得星期五。

或者,如果您已经有星期五列表,请仅返回那些在下个月添加7天的日期。

4

这是我们正在使用的扩展方法。

public static class DateTimeExtensions 
{ 
    public static DateTime GetLastFridayInMonth(this DateTime date) 
    { 
     var firstDayOfNextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1); 
     int vector = (((int)firstDayOfNextMonth.DayOfWeek + 1) % 7) + 1; 
     return firstDayOfNextMonth.AddDays(-vector); 
    } 
} 

下面是MbUnit的测试案例

[TestFixture] 
public class DateTimeExtensionTests 
{ 
    [Test] 
    [Row(1, 2011, "2011-01-28")] 
    [Row(2, 2011, "2011-02-25")] 
    ... 
    [Row(11, 2011, "2011-11-25")] 
    [Row(12, 2011, "2011-12-30")] 
    [Row(1, 2012, "2012-01-27")] 
    [Row(2, 2012, "2012-02-24")] 
    ... 
    [Row(11, 2012, "2012-11-30")] 
    [Row(12, 2012, "2012-12-28")] 

    public void Test_GetLastFridayInMonth(int month, int year, string expectedDate) 
    { 
    var date = new DateTime(year, month, 1); 
    var expectedValue = DateTime.Parse(expectedDate); 

    while (date.Month == month) 
    { 
     var result = date.GetLastFridayInMonth(); 
     Assert.AreEqual(expectedValue, result); 
     date = date.AddDays(1); 
    } 
    } 
} 
-1

调用这个函数的发送日期为参数,在它从日期中提取参数的年份和月份,然后返回,最后Friday一个月

public DateTime GetLastFridayOfMonth(DateTime dt) 
{ 
     DateTime dtMaxValue = DateTime.MaxValue; 
     DateTime dtLastDayOfMonth = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month)); 

     while (dtMaxValue == DateTime.MaxValue) 
     { 
      // Returns if the decremented day is the fisrt Friday from last(ie our last Friday) 
      if (dtMaxValue == DateTime.MaxValue && dtLastDayOfMonth.DayOfWeek == DayOfWeek.Friday) 
       return dtLastDayOfMonth; 
      // Decrements last day by one 
      else 
       dtLastDayOfMonth = dtLastDayOfMonth.AddDays(-1.0); 
     } 
     return dtLastDayOfMonth; 
} 
+0

为什么在这里使用DateTime.MaxValue? – ColinM 2016-12-28 12:11:21

+0

@ColinM它似乎被用作常量。这与'while(true){...}'是一样的,虽然看起来很笨重。它还包含一个不必要的检查,以确定值是否已更改。 – 2017-11-07 12:40:08

1

刚上萨拉特的答案小的改进,对于那些(像我)谁踏进这个问题

private DateTime GetLastFridayOfTheMonth(DateTime date) 
{ 
    var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); 

    while (lastDayOfMonth.DayOfWeek != DayOfWeek.Friday) 
     lastDayOfMonth = lastDayOfMonth.AddDays(-1); 

    return lastDayOfMonth; 
} 
相关问题