Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last Friday of month(s) using .NET

Tags:

c#

.net

c#-3.0

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

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".

I am calling the function as:

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.

like image 974
Newbie Avatar asked Apr 26 '10 05:04

Newbie


3 Answers

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.

like image 93
danish Avatar answered Sep 20 '22 21:09

danish


Just a small improvement on Sarath's answer, for those (like me) who step into this question

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;
}
like image 34
DeBorges Avatar answered Sep 22 '22 21:09

DeBorges


Here's an extension method we are using.

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

Below is the MbUnit test case

[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);
    }
  }
}
like image 34
Mark Avatar answered Sep 21 '22 21:09

Mark