Get first week day of specific year-month - c#

How can I get the date of for example 'first wednesday of april 2013' using c# .net 2.0 ?
is there any helper methods for this kind of job in .net or should I write my own helper method? If there is no method for this kind of job please help me out for writing my own method.
DateTime GetFirstXDayFromY(string dayName, DateTime targetYearMonth)
{
///???
}

public static DateTime GetFirstDay(int year, int month, DayOfWeek day)
{
DateTime result = new DateTime(year, month, 1);
while (result.DayOfWeek != day)
{
result = result.AddDays(1);
}
return result;
}
If you were on .net >= 3.5 you could use Linq:
public static DateTime GetFirstDay(int year, int month, DayOfWeek dayOfWeek)
{
return Enumerable.Range(1, 7).
Select(day => new DateTime(year, month, day)).
First(dateTime => (dateTime.DayOfWeek == dayOfWeek));
}

The .NET Framework makes it easy to determine the ordinal day of the week for a particular date, and to display the localized weekday name for a particular date.
http://msdn.microsoft.com/en-us/library/bb762911.aspx

Please try with the below code snippet.
// Get the Nth day of the month
private static DateTime NthOf(DateTime CurDate, int Occurrence, DayOfWeek Day)
{
var fday = new DateTime(CurDate.Year, CurDate.Month, 1);
if (Occurrence == 1)
{
for (int i = 0; i < 7; i++)
{
if (fday.DayOfWeek == Day)
{
return fday;
}
else
{
fday = fday.AddDays(1);
}
}
return fday;
}
else
{
var fOc = fday.DayOfWeek == Day ? fday : fday.AddDays(Day - fday.DayOfWeek);
if (fOc.Month < CurDate.Month) Occurrence = Occurrence + 1;
return fOc.AddDays(7 * (Occurrence - 1));
}
}
How to call/use them?
NthOf(targetYearMonth, 1, DayOfWeek.Wednesday)

With the help of answers of #vc and #Jayesh I've come up with this method. Thanks a lot.
public static DateTime GetFirstDay(int year, int month, DayOfWeek day, int occurance)
{
DateTime result = new DateTime(year, month, 1);
int i = 0;
while (result.DayOfWeek != day || occurance != i)
{
result = result.AddDays(1);
if((result.DayOfWeek == day))
i++;
}
return result;
}

Related

How do I find the nth DayOfWeek for a given month?

I am trying to find the nth DayOfWeek for a given month (in a given year).
For example: I am looking for the 3rd Saturday of May (2019).
I failed to come up with a working solution using the DayOfWeek extension method. Do I have to loop through the entire month to find the third Saturday?
You could of course loop through the entire month but I think this is a more elegant way (taken from here):
private static DateTime FindTheNthDayOfWeek(int year, int month, int nthOccurrence, DayOfWeek dayOfWeek)
{
if (month < 1 || month > 12)
{
throw new ArgumentOutOfRangeException("Invalid month");
}
if (nthOccurrence < 0 || nthOccurrence > 5)
{
throw new ArgumentOutOfRangeException("Invalid nth occurrence");
}
var dt = new DateTime(year, month, 1);
while (dt.DayOfWeek != dayOfWeek)
{
dt = dt.AddDays(1);
}
dt = dt.AddDays((nthOccurrence - 1) * 7);
if (dt.Month != month)
{
throw new ArgumentOutOfRangeException(string.Format("The given month has less than {0} {1}s", nthOccurrence, dayOfWeek));
}
return dt;
}
This private method doesn't loop through the entire month but stops already once the first DayOfWeek has been found. Then you simply add a week for each nth occurrence (minus the already added week ;-) ).
If you talk about dates it should be related to some calendar, in my example Gregorian.
public static class DataTimeExt
{
public static IEnumerable<DateTime> TakeWhileInclusive(this DateTime value,
Func<DateTime, bool> func)
{
DateTime dt = value;
yield return dt; //[first
while (func(dt = dt.AddDays(1))) yield return dt; //in between
yield return dt; //last]
}
}
then you could just iterate through the dates until Sunday and then add 14 days.
var calendar = new GregorianCalendar();
var dates = new DateTime(2019, 5, 1, calendar)
.TakeWhileInclusive(dt => calendar.GetDayOfWeek(dt) != DayOfWeek.Sunday);
Console.WriteLine(dates.Last().AddDays(14));
This is simple and clean with no looping. Just a little arithmetic.
static DateTime? NthWeekDayOfMonth( int n, DayOfWeek dow, int year , int month)
{
DateTime startOfMonth = new DateTime( year, month, 1 ) ;
int offset = ( 7 + dow - startOfMonth.DayOfWeek ) % 7 ;
DateTime nthWeekDayOfMonth = startOfMonth
.AddDays( offset )
.AddDays( 7 * (n-1) )
;
bool isSameMonth = startOfMonth.Year == nthWeekDayOfMonth.Year
&& startOfMonth.Month == nthWeekDayOfMonth.Month
;
return isSameMonth
? nthWeekDayOfMonth
: (DateTime?) null
;
}
I created two extension methods, where one gets the next DayOfWeek from the date, optionally including the date itself, and the other for the previous DayOfWeek, with the same functionality.
public static DateTime Next(
this DateTime source,
DayOfWeek dayOfWeek,
bool considerSameDate
) => ( dayOfWeek - source.DayOfWeek) is var difference
&& difference < (considerSameDate ? 0 : 1)
? source.AddDays(difference + 7)
: source.AddDays(difference)
;
and
public static DateTime Previous(
this DateTime source,
DayOfWeek dayOfWeek,
bool considerSameDate
) => dayOfWeek == source.DayOfWeek
? ( considerSameDate ? source : source.AddDays(-7) )
: source.AddDays(
( dayOfWeek - source.DayOfWeek ) is var difference
&& difference > 0
? difference - 7
: difference
);
Having these, one can ask the questions you posed:
var x = new System.DateTime(2019, 5, 1).Next(System.DayOfWeek.Saturday, true).AddDays(14);
I create a new DateTime (2019-05-01), call Next with Saturday and consider 5/1 as a candidate, and then add 14 days, which makes it to the third Saturday of May, 2019.

Get Last Date of Given Month and Year in c#

i want c# code for getting Last Date of week for given month and year.
suppose given month is 1 and year is 2016 then method should return me
--01/02/2016
--01/09/2016
--01/16/2016
--01/23/2016
--01/30/2016
--02/06/2016
So you want a method which takes a year and a month as parameter and returns dates. Those dates should be the last dates of all weeks in that month, optionally also of following months.
This should work then:
public static IEnumerable<DateTime> GetLastWeekDatesOfMonth(int year, int month, DayOfWeek firstDayOfWeek = DayOfWeek.Monday, bool includeLaterMonths = false)
{
DateTime first = new DateTime(year, month, 1);
int daysOffset = (int)firstDayOfWeek - (int)first.DayOfWeek;
if (daysOffset < 0)
daysOffset = 7 - Math.Abs(daysOffset);
DateTime firstWeekDay = first.AddDays(daysOffset);
DateTime current = firstWeekDay.AddDays(-1); // last before week start
if (current.Month != month)
current = current.AddDays(7);
yield return current;
if (includeLaterMonths)
{
while (true)
{
current = current.AddDays(7);
yield return current;
}
}
else
{
while((current = current.AddDays(7)).Month == month)
yield return current;
}
}
Your sample:
var lastDates = GetLastWeekDatesOfMonth(2016, 1, DayOfWeek.Sunday, true);
foreach (DateTime dt in lastDates.Take(6))
Console.WriteLine(dt.ToShortDateString());
public static DateTime GetLastDateofWeek(int yr, int mnth, int week)
{
DateTime dt = new DateTime(yr, mnth, 1);
DateTime newdate = new DateTime();
if (dt.DayOfWeek == DayOfWeek.Monday)
{
newdate = dt.AddDays(((week - 1) * 7) + 5);
}
else
{
newdate = dt.AddDays((8 - (int)dt.DayOfWeek) % 7 + ((week - 2) * 7) + 5);
}
return newdate;
}
First get month and year, example:
int year = 2016;
int month = 1;
And then create a new instance of the DateTime class that represents the first saturday.
DateTime firstsaturday = new DateTime(year,month,1);
while(firstsaturday.DayOfWeek != DayOfWeek.Saturday)
{
firstsaturday = firstsaturday.AddDays(1);
]
Then create a List of DateTime values.
List<DateTime> saturdays = new List<DateTime>();
saturdays.Add(firstsaturday);
And then cycle through all saturdays using a loop.
DateTime CurrentSaturday = firstsaturday;
while(CurrentSaturday.AddDays(7).Month == month)
{
CurrentSaturday = CurrentSaturday.AddDays(7);
Saturdays.Add(CurrentSaturday);
}
You may try this code.
using System;
public class Program
{
public static void Main()
{
DateTime thisMonthInLastYear = DateTime.Now.AddYears(-1);
DateTime endOfMonth = new DateTime(thisMonthInLastYear.Year,
thisMonthInLastYear.Month,
DateTime.DaysInMonth(thisMonthInLastYear.Year,
thisMonthInLastYear.Month));
Console.WriteLine("Today : "+DateTime.Now.ToString("dd-MM-yyyy"));
Console.WriteLine("This Month in last years : "+endOfMonth.ToString("dd-MM-yyyy"));
Console.WriteLine("Next month in last years : "+endOfMonth.AddMonths(1).ToString("dd-MM-yyyy"));
}
}

C# check if current date is 1, 2 or 3 of the month, ignore weekend and additional List<DateTime>

I need to check if DateTime.Now is in the first 3 business days of each month (from Mon - Fri). I also need to provide a List<DateTime> with national holidays and these should be handled accordingly.
If DateTime.Now is Saturday and is 1 of the month, first 3 business days are Monday, Tuesday, Wednesday (3, 4, 5 of the month).
public bool IsBusinessDay()
{
DateTime now = DateTime.Now;
DateTime fbd = new DateTime();
DateTime sbd = new DateTime();
DateTime tbd = new DateTime();
DateTime fm = new DateTime(now.Year, now.Month, 1);
DateTime sm = new DateTime(now.Year, now.Month, 2);
DateTime tm = new DateTime(now.Year, now.Month, 3);
// first business day
if (fm.DayOfWeek == DayOfWeek.Sunday)
{
fbd = fm.AddDays(1);
}
else if (fm.DayOfWeek == DayOfWeek.Saturday)
{
fbd = fm.AddDays(2);
}
else
{
fbd = fm;
}
//second business day
if (sm.DayOfWeek == DayOfWeek.Sunday)
{
sbd = sm.AddDays(1);
}
else if (sm.DayOfWeek == DayOfWeek.Saturday)
{
sbd = sm.AddDays(2);
}
else
{
sbd = sm;
}
//third business day
if (tm.DayOfWeek == DayOfWeek.Sunday)
{
tbd = tm.AddDays(1);
}
else if (tm.DayOfWeek == DayOfWeek.Saturday)
{
tbd = tm.AddDays(2);
}
else
{
tbd = tm;
}
if (now == fdb || now == sbd || now == tbd)
{
return true;
}
return false;
}
Is this a good approach? How can I add a List<DateTime> with holidays and check that the current date is not holiday?
I have a feeling I'm over thinking this, and thinking it in a bad way. I don't know why but same feeling tells me there is an easier way to do it.
This should do what you want. You'll have to supply the set of holidays.
public static bool IsFirstThreeBusinessDays(DateTime date, HashSet<DateTime> holidays)
{
DateTime dt = new DateTime(date.Year, date.Month, 1);
int businessDaysSeen = 0;
while (businessDaysSeen < 3)
{
if (dt.DayOfWeek != DayOfWeek.Saturday &&
dt.DayOfWeek != DayOfWeek.Sunday &&
!holidays.Contains(dt))
{
if (dt == date.Date)
{
return true;
}
businessDaysSeen++;
}
dt = dt.AddDays(1);
}
return false;
}
You can also do this using LINQ.
public static bool IsFirstThreeBusinessDays(DateTime date, HashSet<DateTime> holidays)
{
var query =
Enumerable.Range(1, DateTime.DaysInMonth(date.Year, date.Month))
.Select(o => new DateTime(date.Year, date.Month, o))
.Where(o => o.DayOfWeek != DayOfWeek.Saturday && o.DayOfWeek != DayOfWeek.Sunday
&& !holidays.Contains(o))
.Take(3);
return query.Contains(date);
}
EDIT: I didn't read the question carefully enough, although parts of my old answer are still applicable. The approach I would take here is to create a method that enumerates the business days of the month, then take 3 from that.
Here's how:
public static IEnumerable<DateTime> BusinessDaysOfMonth(DateTime time)
{
var month = new DateTime(time.Year, time.Month, 1);
var nextMonth = month.AddMonths(1);
var current = month;
while(current < nextMonth)
{
if (IsWeekday(current) && !IsHoliday(current))
{
yield return current;
}
current = current.AddDays(1);
}
}
(note that some methods are taken from below). Then, all you need where you want to use this is:
// Get first three business days
var firstThreeBizDays = BusinessDaysOfMonth(DateTime.Now).Take(3);
// Check if today is one of them
var result = firstThreeBizDays.Contains(DateTime.Today);
OLD ANSWER:
Ok, so it looks like there's three conditions you need to ensure. They are:
It is the 1st, 2nd, or 3rd day of the month
It is not Saturday or Sunday
The current date is not contained in some set of dates representing holidays
This translates fairly straightforwardly to code:
public static bool IsFirstThreeDays(DateTime time) => time.Day < 4;
public static bool IsWeekday(DateTime time)
{
var dow = time.DayOfWeek;
return dow != DayOfWeek.Saturday && dow != DayOfWeek.Sunday;
}
public bool IsHoliday(DateTime time)
{
ISet<DateTime> holidays = ??; // Decide whether this is a member or an arg
return holidays.Contains(time.Date);
}
Note that the holidays set needs to contain the Day component of any DateTime from each holiday.
Now your method is presumably just:
public static bool IsDayWhatYouWant()
{
var now = DateTime.UtcNow;
return IsFirstThreeDays(now) && IsWeekday(now) && !IsHoliday(now);
}

How to get last Friday of month(s) using .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;
}
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;
}

C# - What is the best way to get a list of the weeks in a month, given a starting weekday?

I need to get a list of weeks for a given month, with Monday as the start day.
So for example, for the month of February 2009, this method would return:
2/2/2009
2/9/2009
2/16/2009
2/23/2009
// Get the weeks in a month
DateTime date = DateTime.Today;
// first generate all dates in the month of 'date'
var dates = Enumerable.Range(1, DateTime.DaysInMonth(date.Year, date.Month)).Select(n => new DateTime(date.Year, date.Month, n));
// then filter the only the start of weeks
var weekends = from d in dates
where d.DayOfWeek == DayOfWeek.Monday
select d;
public static List<DateTime> GetWeeks(
this DateTime month, DayOfWeek startOfWeek)
{
var firstOfMonth = new DateTime(month.Year, month.Month, 1);
var daysToAdd = ((Int32)startOfWeek - (Int32)month.DayOfWeek) % 7;
var firstStartOfWeek = firstOfMonth.AddDays(daysToAdd);
var current = firstStartOfWeek;
var weeks = new List<DateTime>();
while (current.Month == month.Month)
{
weeks.Add(current);
current = current.AddDays(7);
}
return weeks;
}
Here's a solution (effectively one line) using C# 3.0/LINQ, in case you're interested:
var month = new DateTime(2009, 2, 1);
var weeks = Enumerable.Range(0, 4).Select(n => month.AddDays(n * 7 - (int)month.DayOfWeek + 1)).TakeWhile(monday => monday.Month == month.Month);
int year = 2009;
int month = 2;
DateTime startDate = new DateTime(year, month, 1);
DateTime endDate = startDate.AddMonths(1);
while (startDate.DayOfWeek != DayOfWeek.Monday)
startDate = startDate.AddDays(1);
for (DateTime result = startDate; result < endDate; result = result.AddDays(7))
DoWhatYouWant(result);
How about this?
public IEnumerable<DateTime> GetWeeks(DateTime date, DayOfWeek startDay)
{
var list = new List<DateTime>();
DateTime first = new DateTime(date.Year, date.Month, 1);
for (var i = first; i < first.AddMonths(1); i = i.AddDays(1))
{
if (i.DayOfWeek == startDay)
list.Add(i);
}
return list;
}
Something like the following pseudo-code should work:
Determine the start date of the month (use month and year from a date and set the day to 1
Determine the end date of the month (start date + 1 month)
Determine the first date that is a monday (this is your first item in the list)
Add 7 days to find the next date and repeat until you read or pass the month end
Just change the response line to what ever you need to do with it
protected void PrintDay(int year, int month, DayOfWeek dayName)
{
CultureInfo ci = new CultureInfo("en-US");
for (int i = 1 ; i <= ci.Calendar.GetDaysInMonth (year, month); i++)
{
if (new DateTime (year, month, i).DayOfWeek == dayName)
Response.Write (i.ToString() + "<br/>");
}
}
Quick solution: i don't think there is a built in function for it....
I see you got your answer, but I wanted to share with you a helper class I created for one of my projects. It's far to be a comprehansive class, but might help...
public static class WeekHelper {
#region Public Methods
public static DateTime GetWeekStart(DateTime date) {
DateTime weekStart;
int monday = 1;
int crtDay = (int)date.DayOfWeek;
if (date.DayOfWeek == DayOfWeek.Sunday)
crtDay = 7;
int difference = crtDay - monday;
weekStart = date.AddDays(-difference);
return weekStart;
}
public static DateTime GetWeekStop(DateTime date) {
DateTime weekStart;
int sunday = 7;
int crtDay = (int)date.DayOfWeek;
if (date.DayOfWeek == DayOfWeek.Sunday)
crtDay = 7;
int difference = sunday - crtDay;
weekStart = date.AddDays(difference);
return weekStart;
}
public static void GetWeekInterval(int year, int weekNo,
out DateTime weekStart, out DateTime weekStop) {
GetFirstWeekOfYear(year, out weekStart, out weekStop);
if (weekNo == 1)
return;
weekNo--;
int daysToAdd = weekNo * 7;
DateTime dt = weekStart.AddDays(daysToAdd);
GetWeekInterval(dt, out weekStart, out weekStop);
}
public static List<KeyValuePair<DateTime, DateTime>> GetWeekSeries(DateTime toDate) {
//gets week series from beginning of the year
DateTime dtStartYear = new DateTime(toDate.Year, 1, 1);
List<KeyValuePair<DateTime, DateTime>> list = GetWeekSeries(dtStartYear, toDate);
if (list.Count > 0) {
KeyValuePair<DateTime, DateTime> week = list[0];
list[0] = new KeyValuePair<DateTime, DateTime>(dtStartYear, week.Value);
}
return list;
}
public static List<KeyValuePair<DateTime, DateTime>> GetWeekSeries(DateTime fromDate, DateTime toDate) {
if (fromDate > toDate)
return null;
List<KeyValuePair<DateTime, DateTime>> list = new List<KeyValuePair<DateTime, DateTime>>(100);
DateTime weekStart, weekStop;
toDate = GetWeekStop(toDate);
while (fromDate <= toDate) {
GetWeekInterval(fromDate, out weekStart, out weekStop);
list.Add(new KeyValuePair<DateTime, DateTime>(weekStart, weekStop));
fromDate = fromDate.AddDays(7);
}
return list;
}
public static void GetFirstWeekOfYear(int year, out DateTime weekStart, out DateTime weekStop) {
DateTime date = new DateTime(year, 1, 1);
GetWeekInterval(date, out weekStart, out weekStop);
}
public static void GetWeekInterval(DateTime date,
out DateTime dtWeekStart, out DateTime dtWeekStop) {
dtWeekStart = GetWeekStart(date);
dtWeekStop = GetWeekStop(date);
}
#endregion Public Methods
}
This works beautifully! All you have to do is get the first day of the month you want to get the weeks for and then this will give you the first day of every week. You need to get 5 weeks (not 4) so the Enumerable.Range counts out 5 instead of 4.
var date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
var weeks = from n in Enumerable.Range(0, 5)
select date.AddDays(7 * n + (-1 * (int)date.DayOfWeek));
Here's what i did, using Chaowlert's code as a starting base. Basically i modified that you need to check if adding the days in the for overflows to the next month, so i don't add 4 days (monday to friday), but actually the minimum between 4 and the number of remaining days in the month. Also, i check if the current day is a weekend, otherwise add days until it's a weekday. My purpose is to print the weeks in a month, from monday to friday
DateTime fechaInicio = new DateTime(año, mes, 1);
DateTime fechaFin = fechaInicio.AddMonths(1);
int diasHastaFinMes = 0;
while (esFinDeSemana(fechaInicio))
fechaInicio = fechaInicio.AddDays(1);
for (DateTime fecha = fechaInicio; fecha < fechaFin; fecha = fecha.AddDays(7))
{
diasHastaFinMes = DateTime.DaysInMonth(fecha.Year, fecha.Month) - fecha.Day;
printWeeks(fecha, fecha.AddDays(Math.Min(4, diasHastaFinMes)));
}

Categories