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.
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.
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;
}
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);
}
}
}
Check what day of the week the first day of the next month is on, then subtract enough days to get a Friday.
Or, if you already have a list of Fridays, return only those for which adding 7 days gives a date in the next month.
Based on DeBorges answer, here is an extension to get any specific Day
public static DateTime GetLastSpecificDayOfTheMonth(this DateTime date, DayOfWeek dayofweek)
{
var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
while (lastDayOfMonth.DayOfWeek != dayofweek)
lastDayOfMonth = lastDayOfMonth.AddDays(-1);
return lastDayOfMonth;
}
Call the below function by sending the date as parameter, in which it extracts the month and year from the date parameter and returns the last Friday
of that month
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;
}
精彩评论