如何获得使用.NET个月(县)上周五 [英] How to get last Friday of month(s) using .NET

查看:120
本文介绍了如何获得使用.NET个月(县)上周五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,返回我只有周五的日期范围

I have a function that returns me only the fridays from a range of dates

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

函数的目的:的历史可以追溯到指定到起始日期的星期数列表,即如果起始日期为2010年4月23日的周数是1,那么程序应该返回的日期2010年4月16日,直到startddate。

Purpose of the function: "List of dates from the Week number specified till the StartDate i.e. If startdate is 23rd April, 2010 and the week number is 1,then the program should return the dates from 16th April, 2010 till the startddate".

我打电话的功能:

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

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

Now the requirement has changed a bit. I need to find out only the last Fridays of every month. The input to the function will remain same.

推荐答案

您已经有了周五在给定范围内的清单。现在,只需再次查询这个是这样的:

You already have the list of Fridays in the given range. Now just query this again like this:

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

希望这有助于。

Hope this helps.

这篇关于如何获得使用.NET个月(县)上周五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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