Combine Monday to Sunday dates to textfile - c#

This is a C# 4.0 WinForms app.
I'm trying to pull all the Monday dates in a given month (the fully spelled out Month name and day), and the days in all the Sundays. Then, using StreamWriter, write these to a text file.
I found the following code suggestion on enumerating through a month, on StackOverFlow below.
How to get every monday of given month?
public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
for (int day = 1; day <= days; day++)
{
yield return new DateTime(year, month, day);
}
}
And then you can call with LINQ like so:
var mondays = AllDatesInMonth(2017, 7).Where(i => i.DayOfWeek == DayOfWeek.Monday);
I modified the suggestion to also pull the Sunday dates. I'm testing this, in order to make it part of a larger application.
This is what I've come up with so far, as shown below.
private void btnTestDayOWeek_Click(object sender, EventArgs e)
{
int curYear = DateTime.Now.Year;
int month = DateTime.Now.Month;
string outputFile = #"C:\dates2Compare.txt";
List<string> mondayList = new List<string>();
List<string> sundayList = new List<string>();
using (StreamWriter sw = new StreamWriter(outputFile))
{
var mondays = AllDatesInMonth(curYear, 3).Where(i => i.DayOfWeek == DayOfWeek.Monday);
var sundays = AllDatesInMonth(curYear, 3).Where(i => i.DayOfWeek == DayOfWeek.Sunday);
foreach (DateTime Monday in mondays)
{
mondayList.Add(Monday.ToString("MMMM d"));
}
foreach (DateTime Sunday in sundays)
{
sundayList.Add(Sunday.ToString("-d"));
}
var dayList = mondayList.Concat(sundayList);//attempt to join these 2-Lists
foreach (string dt in dayList)
{
sw.WriteLine(dt);
}
}
}
public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
for (int day = 1; day <= days; day++)
{
yield return new DateTime(year, month, day);
}
}
I need to join the Monday to Sunday dates, like this, i.e.
March 1-7
March 8-14
But this is what I'm receiving so far.
March 1
March 8
March 15
March 22
March 29
-7
-14
-21
-28
Can someone suggest how to modify my code so that, the text file that is written to, correctly displays the Monday date with a "-" after it and then the Sunday date?

You are making it too complicated. Simply loop the days of the month in a split second:
int year = 2021;
int month = 3;
DateTime date = new DateTime(year, month, 1);
string monthName = date.ToString("MMMM");
int ultimo = date.AddMonths(1).AddDays(-1).Day;
for (int i = 0; i < ultimo - 6; i++)
{
DateTime monthDate = date.AddDays(i);
if (monthDate.DayOfWeek == DayOfWeek.Monday)
{
string line = string.Format(monthName + " {0}-{1}", monthDate.Day, monthDate.Day + 6);
Console.WriteLine(line);
// sw.WriteLine(line);
}
}
Output:
March 1-7
March 8-14
March 15-21
March 22-28

//var dayList = mondayList.Concat(sundayList);//attempt to join these 2-Lists
List<string> dayList = new List<string>();
int weekCount = Math.Min(mondayList.Count(), sundayList.Count());
for (int i = 0; i < weekCount; i++)
{
string concateValue = mondayList[i] + sundayList[i];
dayList.Add(concateValue);
}
When you Concat lists, the values stack on top of each other. You need to merge the lists together.

Related

Get all the dates of the month from Day of week

Is there any way to get all the occuring date for the particular day of the specified month and year..
For example:- If I have Day Thursday then I need all the dates of December 2016. Then collection should contain dates 1/12/2016, 8/12/2016, 15/12/2016, 22/12/2016, 29/12/2016.
You can create a function to get the list of dates of the month based on day of a particular year like
public static List<DateTime> GetDates(int year, int month,string day)
{
return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Where(d=>new DateTime(year, month, d).ToString("dddd").Equals(day))
.Select(d => new DateTime(year, month, d)).ToList();
}
Now call this function like
var dates=GetDates(2016,12,"Thursday");
foreach(var d in dates){
Console.WriteLine(d.ToString());
}
Output will be
12/1/2016 12:00:00 AM
12/8/2016 12:00:00 AM
12/15/2016 12:00:00 AM
12/22/2016 12:00:00 AM
12/29/2016 12:00:00 AM
Now you have complete list of dates based on a day. You can further use it based on your requirements.
DateTime decFirst = new DateTime(2016, 12, 1);
int offset = (int)decFirst.DayOfWeek;
DateTime decThursday = decFirst.AddDays(offset);
Now you have the first Thursday of December, you should be able to do the rest.
Long form version...
private void button1_Click(object sender, EventArgs e)
{
List<DateTime> Thursdays = DaysOfMonth(2016, 12, DayOfWeek.Thursday);
foreach(DateTime dt in Thursdays)
{
Console.WriteLine(dt.ToString());
}
}
public static List<DateTime> DaysOfMonth(int year, int month, DayOfWeek day)
{
List<DateTime> dates = new List<DateTime>();
for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++)
{
DateTime dt = new DateTime(year, month, i);
if (dt.DayOfWeek == day)
{
dates.Add(dt);
}
}
return dates;
}

Get List of 1st and 3rd Saturday of month if 2nd Saturday Appear in first 9 days

I want to get List of all 2nd & 4th Saturdays of an year. But if 2nd Saturday appear in first 9 days of a month then return list of 3rd and 5th Saturday of that month. So far i get list of all 2nd & 4th Saturday of whole year. But I'm not able to get 3rd and 5th Saturday if 2nd Saturday appear in first 9 days of a month.
class Program
{
static void Main(string[] args)
{
List<DateTime> MyCalendar = new List<DateTime>(); //create list
DateTime currDate = new DateTime(2017, 1, 1); //initial value
//add days to MyCalendar
while (currDate <= new DateTime(2017, 12, 31))
{
MyCalendar.Add(currDate);
currDate = currDate.AddDays(1);
}
//method to get 2. and 4. saturday in month
var result = MyCalendar.Where(x => x.DayOfWeek == DayOfWeek.Saturday)
.GroupBy(x => x.Month)
.SelectMany(grp =>
grp.Select((d, counter) => new
{
Month = grp.Key,
PosInMonth = counter + 1,
Day = d
}))
.Where(x => x.PosInMonth == 2 || x.PosInMonth == 4)
.ToList();
foreach (var d in result)
{
Console.WriteLine("{0} {1} {2}", d.Month, d.PosInMonth, d.Day);
}
Console.Read();
}
}
Out Put of the Program
Out of Program
Another method
DateTime currDate = new DateTime(2017, 1, 1); //initial value
int dayOfWeek = (int)currDate.DayOfWeek;
currDate = currDate.AddDays(6 - dayOfWeek);
//add days to MyCalendar
while (currDate <= new DateTime(2017, 12, 31))
{
MyCalendar.Add(currDate);
currDate = currDate.AddDays(7);
}
var result = MyCalendar.GroupBy(x => x.Month).Select(x => x.Skip(1).First().Day <= 9 ? new DateTime[] { x.Skip(2).First(), x.Skip(4).First() } : new DateTime[] { x.Skip(1).First(), x.Skip(3).First() }).SelectMany(x => x).ToList();
foreach (var d in result)
{
Console.WriteLine("{0} {1}", d.Month, d.Day);
}
Console.Read();
Here is a quick and dirty solution; I admit that it could be more optimized.
Method which will do the lookup:
private static IEnumerable<DateTime> GetWeekDayOfMonth(DateTime monthToCheck, DayOfWeek weekDayToFind)
{
var year = monthToCheck.Year;
var month = monthToCheck.Month;
var dayCount = DateTime.DaysInMonth(year, month);
var daysList = Enumerable.Range(1, dayCount)
.Select(day => new DateTime(year, month, day))
.Where(date => date.DayOfWeek == weekDayToFind)
.ToList<DateTime>();
// Loop with 2 increment
int lookupStart = 1;
int loopCount = 0;
if (daysList[1].Day <= 9)
{
lookupStart = 2;
}
for (var i = lookupStart; i < daysList.Count(); i = i + 2)
{
if (loopCount < 2)
{
yield return daysList[i];
loopCount++;
}
}
}
Here is the code to call it:
private static void FindWeekDays()
{
DateTime dateToCheck = new DateTime(2017, 1, 1);
List<DateTime> dayList = new List<DateTime>();
while (dateToCheck.Year <= 2017)
{
dayList.AddRange(GetWeekDayOfMonth(dateToCheck, DayOfWeek.Saturday));
dateToCheck = dateToCheck.AddMonths(1);
}
dayList.ForEach(date => Console.WriteLine(date.ToString()));
}
And the Main:
static void Main(string[] args)
{
FindWeekDays();
}
How about this?
using System;
using System.Collections.Generic;
namespace Demo
{
class Program
{
static void Main()
{
foreach (var saturday in FilteredSaturdays(DateTime.Now, new DateTime(2017, 12, 31)))
{
Console.WriteLine(saturday);
}
}
public static IEnumerable<DateTime> FilteredSaturdays(DateTime start, DateTime end)
{
DateTime startMonth = new DateTime(start.Year, start.Month, 1);
DateTime endMonth = new DateTime(end.Year, end.Month, 1).AddMonths(1);
for (DateTime month = startMonth; month < endMonth; month = month.AddMonths(1))
{
// Work out date of last saturday in the month.
DateTime lastDayOfMonth = month.AddMonths(1).AddDays(-1);
DateTime lastSaturdayOfMonth = lastDayOfMonth.AddDays(-(((int)lastDayOfMonth.DayOfWeek+1)%7));
// Return saturday 2 weeks before last saturday of month, and last saturday of month.
yield return lastSaturdayOfMonth.AddDays(-14);
yield return lastSaturdayOfMonth;
}
}
}
}
This is going to be more efficient than looking at every single day of the year to see if it's a Saturday!
[EDIT] It seems that the actual requirement is that you want the last Saturday in every month and the Saturday two weeks before that Saturday, so I have updated my solution accordingly.

Getting year and week from date

I need to return year and week of a given date. Sounds simple. But to be right 2012-01-01 have to return 2011-52, because week 1 in 2012 starts January 2th.
To find the week, I use:
GregorianCalendar calw = new GregorianCalendar(GregorianCalendarTypes.Localized);
return calw.GetWeekOfYear(DateTime.Parse("2012-01-01"), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday).ToString();
this return 52. (correct)
But how do I get the Year?
edit:
With the help from here: http://codebetter.com/petervanooijen/2005/09/26/iso-weeknumbers-of-a-date-a-c-implementation/
This seems to work:
private int weekYear(DateTime fromDate)
{
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
int week = weekNumber(fromDate);
int month = cal.GetMonth(fromDate);
int year = cal.GetYear(fromDate);
//week starts after 31st december
if (week > 50 && month == 1)
year = year - 1;
//week starts before 1st January
if (week < 5 && month == 12)
year = year + 1;
return year;
}
private int weekNumber(DateTime fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDays(-fromDate.Day + 1).AddMonths(-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
// ISO 8601 weeks start with Monday
// The first week of a year includes the first Thursday
// DayOfWeek returns 0 for sunday up to 6 for saterday
int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return weekNumber(startOfYear.AddDays(-1));
case 53:
// If dec 31st falls before thursday it is week 01 of next year
if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
return 1;
else
return wk;
default: return wk;
}
}
Noda Time handles this for you very easily:
Noda Time v1.x
using System;
using NodaTime;
public class Test
{
static void Main()
{
LocalDate date = new LocalDate(2012, 1, 1);
Console.WriteLine($"WeekYear: {date.WeekYear}"); // 2011
Console.WriteLine($"WeekOfWeekYear: {date.WeekOfWeekYear}"); // 52
}
}
Noda Time v2.x
using System;
using NodaTime;
using NodaTime.Calendars;
public class Test
{
static void Main()
{
LocalDate date = new LocalDate(2012, 1, 1);
IWeekYearRule rule = WeekYearRules.Iso;
Console.WriteLine($"WeekYear: {rule.GetWeekYear(date)}"); // 2011
Console.WriteLine($"WeekOfWeekYear: {rule.GetWeekOfWeekYear(date)}"); // 52
}
}
That's using the ISO calendar system where the week year starts in the first week with at least 4 days in that year. (Like CalendarWeekRule.FirstFourDayWeek.) If you want a different calendar system, specify it in the LocalDate constructor. Week year rules are handled slightly differently between 1.x and 2.x.
EDIT: Note that this gives the right value for both this situation (where the week-year is less than the calendar year) and the situation at the other end of the year, where the week-year can be more than the calendar year. For example, December 31st 2012 is in week 1 of week-year 2013.
That's the beauty of having a library do this for you: its job is to understand this sort of thing. Your code shouldn't have to worry about it. You should just be able to ask for what you want.
You can get the weeknumber according to the CalendarWeekRule in this way:
var d = new DateTime(2012, 01, 01);
System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
var firstDayWeek = cul.Calendar.GetWeekOfYear(
d,
System.Globalization.CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int weekNum = cul.Calendar.GetWeekOfYear(
d,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
int year = weekNum >= 52 && d.Month == 1 ? d.Year - 1 : d.Year;
You probably want to compare CalendarWeekRule.FirstDay with CalendarWeekRule.FirstFourDayWeek. On this way you get the weeknumber and the year (DateTime.Year-1 if they differ).
CultureInfo.Calendar Property
Calendar.GetWeekOfYear Method
CalendarWeekRule Enumeration
That is just an edge case which you will have to add special code for. Get the year from the date string and then if the week = 52 and the month = 1 then subtract one from the year.
I have solving similar problem where the result should be in "YYYYWW" format. I wanted avoid hardcoded dates and using 3rd party libraries.
My test case was date 1.1.2017 which should return week 201652 (Iso YearWeek)
To get week number I have used thread: Get the correct week number of a given date which returns week number without the year.
Finally the correct year I got from Monday(first day of iso week) of required date:
// returns only week number
// from [Get the correct week number of a given date] thread
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
var week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return week;
}
// returns int YearWeek in format "YYYYWW"
public static int GetIso8601YearWeekOfYear(DateTime time)
{
var delta = (-((time.DayOfWeek - CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7));
var firstDayofWeek = time.AddDays(delta); // takeMonday
var week = GetIso8601WeekOfYear(time);
var yearWeek = (firstDayofWeek.Year * 100) + week;
return yearWeek;
}
In my approach I'm taking advantage of the fact, that GetWeekOfYear() displays a correct ISO-8601 week number for days with the same year as Thursday of the same week. So I look for Thursday that belongs to the same week as a given date, and then call GetWeekOfYear() on it.
I can't do that trick to get a correct year, as there's no iso8601-compliant method for this, so I make a year adjustment if Thursday belongs to a different year than a given date.
The solution is basically a three-liner:
using System.Globalization;
namespace TESTS
{
class Program
{
static void Main(string[] args)
{
//sample dates with correct week numbers in comments:
string[] dats = new string[] {
"2011-12-31","2012-01-01" //1152
,"2012-12-31","2013-01-01" //1301
,"2013-12-31","2014-01-01" //1401
,"2014-12-31","2015-01-01" //1501
,"2015-12-31", "2016-01-01" //1553
};
foreach (string str in dats)
{
Console.WriteLine("{0} {1}", str, GetCalendarWeek(DateTime.Parse(str)));
}
Console.ReadKey();
}
public static int GetCalendarWeek(DateTime dat)
{
CultureInfo cult = System.Globalization.CultureInfo.CurrentCulture;
// thursday of the same week as dat.
// value__ for Sunday is 0, so I need (true, not division remainder %) mod function to have values 0..6 for monday..sunday
// If you don't like casting Days to int, use some other method of getting that thursday
DateTime thursday = dat.AddDays(mod((int)DayOfWeek.Thursday-1,7) - mod((int)dat.DayOfWeek-1,7));
//week number for thursday:
int wk = cult.Calendar.GetWeekOfYear(thursday, cult.DateTimeFormat.CalendarWeekRule, cult.DateTimeFormat.FirstDayOfWeek);
// year adjustment - if thursday is in different year than dat, there'll be -1 or +1:
int yr = dat.AddYears(thursday.Year-dat.Year).Year;
// return in yyww format:
return 100 * (yr%100) + wk;
}
// true mod - helper function (-1%7=-1, I need -1 mod 7 = 6):
public static int mod(int x, int m)
{
return (x % m + m) % m;
}
}

Get a list of weeks for a year - with dates

I've been racking my brains over this, but it's late on a Friday and I'm going round in circles.
I need to create a list of working weeks for a drop down list, with the week number as the value. So the code would output:
Monday 22nd August - Friday 26th September
Monday 29th August - Friday 2 September
Monday 5th September - Friday 9 September
etc..
For the whole year. Any ideas how I would achieve this?
Thanks.
I think the code below complies with ISO 8601:
var jan1 = new DateTime(DateTime.Today.Year , 1, 1);
//beware different cultures, see other answers
var startOfFirstWeek = jan1.AddDays(1 - (int)(jan1.DayOfWeek));
var weeks=
Enumerable
.Range(0,54)
.Select(i => new {
weekStart = startOfFirstWeek.AddDays(i * 7)
})
.TakeWhile(x => x.weekStart.Year <= jan1.Year)
.Select(x => new {
x.weekStart,
weekFinish=x.weekStart.AddDays(4)
})
.SkipWhile(x => x.weekFinish < jan1.AddDays(1) )
.Select((x,i) => new {
x.weekStart,
x.weekFinish,
weekNum=i+1
});
Bear in mind, that week calculations are done differently in different cultures and there is not a bug if you see week number 53!
using System.Globalization;
CultureInfo cultInfo = CultureInfo.CurrentCulture;
int weekNumNow = cultInfo.Calendar.GetWeekOfYear(DateTime.Now,
cultInfo.DateTimeFormat.CalendarWeekRule,
cultInfo.DateTimeFormat.FirstDayOfWeek);
Just updating what Spender put, because I wanted to make the output of your Datetimes more towards what you wanted.
DateTime jan1 = new DateTime(DateTime.Today.Year, 1, 1);
//beware different cultures, see other answers
DateTime startOfFirstWeek = jan1.AddDays(1 - (int)(jan1.DayOfWeek));
var weeks=
Enumerable
.Range(0,54)
.Select(i => new {
weekStart = startOfFirstWeek.AddDays(i * 7)
})
.TakeWhile(x => x.weekStart.Year <= jan1.Year)
.Select(x => new {
x.weekStart,
weekFinish=x.weekStart.AddDays(4)
})
.SkipWhile(x => x.weekFinish.Year < jan1.Year)
.Select((x,i) => new {
WeekStart = x.weekStart.ToString("dddd, d, MMMM"),
WeekFinish = x.weekFinish.ToString("dddd, d, MMMM"),
weekNum=i+1
});
The change to correct the formatting to what you wanted is in the last select of the anonymous object.
You can use the Week class of the Time Period Library for .NET:
DateTime start = DateTime.Now.Date;
DateTime end = start.AddYears( 1 );
Week week = new Week( start );
while ( week.Start < end )
{
Console.WriteLine( "week " + week );
week = week.GetNextWeek();
}
You may need to tweak this a bit, but it should get you what you need:
static void Main(string[] args)
{
List<DateTime[]> weeks = new List<DateTime[]>();
DateTime beginDate = new DateTime(2011, 01, 01);
DateTime endDate = new DateTime(2012, 01, 01);
DateTime monday = DateTime.Today;
DateTime friday = DateTime.Today;
while (beginDate < endDate)
{
beginDate = beginDate.AddDays(1);
if (beginDate.DayOfWeek == DayOfWeek.Monday)
{
monday = beginDate;
}
else if (beginDate.DayOfWeek == DayOfWeek.Friday)
{
friday = beginDate;
}
else if (beginDate.DayOfWeek == DayOfWeek.Saturday)
{
weeks.Add(new DateTime[] { monday, friday });
}
}
for (int x = 0; x < weeks.Count; x++)
{
Console.WriteLine(weeks[x][0].Date.ToShortDateString() + " - " + weeks[x][1].Date.ToShortDateString());
}
Console.ReadLine();
}

Count number of Mondays in a given date range

Given a date range, I need to know how many Mondays (or Tuesdays, Wednesdays, etc) are in that range.
I am currently working in C#.
Try this:
static int CountDays(DayOfWeek day, DateTime start, DateTime end)
{
TimeSpan ts = end - start; // Total duration
int count = (int)Math.Floor(ts.TotalDays / 7); // Number of whole weeks
int remainder = (int)(ts.TotalDays % 7); // Number of remaining days
int sinceLastDay = (int)(end.DayOfWeek - day); // Number of days since last [day]
if (sinceLastDay < 0) sinceLastDay += 7; // Adjust for negative days since last [day]
// If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
if (remainder >= sinceLastDay) count++;
return count;
}
Since you're using C#, if you're using C#3.0, you can use LINQ.
Assuming you have an Array/List/IQueryable etc that contains your dates as DateTime types:
DateTime[] dates = { new DateTime(2008,10,6), new DateTime(2008,10,7)}; //etc....
var mondays = dates.Where(d => d.DayOfWeek == DayOfWeek.Monday); // = {10/6/2008}
Added:
Not sure if you meant grouping them and counting them, but here's how to do that in LINQ as well:
var datesgrouped = from d in dates
group d by d.DayOfWeek into grouped
select new { WeekDay = grouped.Key, Days = grouped };
foreach (var g in datesgrouped)
{
Console.Write (String.Format("{0} : {1}", g.WeekDay,g.Days.Count());
}
It's fun to look at different algorithms for calculating day of week, and #Gabe Hollombe's pointing to WP on the subject was a great idea (and I remember implementing Zeller's Congruence in COBOL about twenty years ago), but it was rather along the line of handing someone a blueprint of a clock when all they asked what time it was.
In C#:
private int CountMondays(DateTime startDate, DateTime endDate)
{
int mondayCount = 0;
for (DateTime dt = startDate; dt < endDate; dt = dt.AddDays(1.0))
{
if (dt.DayOfWeek == DayOfWeek.Monday)
{
mondayCount++;
}
}
return mondayCount;
}
This of course does not evaluate the end date for "Mondayness", so if this was desired, make the for loop evaluate
dt < endDate.AddDays(1.0)
Here's some pseudocode:
DifferenceInDays(Start, End) / 7 // Integer division discarding remainder
+ 1 if DayOfWeek(Start) <= DayImLookingFor
+ 1 if DayOfWeek(End) >= DayImLookingFor
- 1
Where DifferenceInDays returns End - Start in days, and DayOfWeek returns the day of the week as an integer. It doesn't really matter what mapping DayOfWeek uses, as long as it is increasing and matches up with DayImLookingFor.
Note that this algorithm assumes the date range is inclusive. If End should not be part of the range, you'll have to adjust the algorithm slightly.
Translating to C# is left as an exercise for the reader.
Any particular language and therefore date format?
If dates are represented as a count of days, then the difference between two values plus one (day), and divide by 7, is most of the answer. If both end dates are the day in question, add one.
Edited: corrected 'modulo 7' to 'divide by 7' - thanks. And that is integer division.
You could try this, if you want to get specific week days between two dates
public List<DateTime> GetSelectedDaysInPeriod(DateTime startDate, DateTime endDate, List<DayOfWeek> daysToCheck)
{
var selectedDates = new List<DateTime>();
if (startDate >= endDate)
return selectedDates; //No days to return
if (daysToCheck == null || daysToCheck.Count == 0)
return selectedDates; //No days to select
try
{
//Get the total number of days between the two dates
var totalDays = (int)endDate.Subtract(startDate).TotalDays;
//So.. we're creating a list of all dates between the two dates:
var allDatesQry = from d in Enumerable.Range(1, totalDays)
select new DateTime(
startDate.AddDays(d).Year,
startDate.AddDays(d).Month,
startDate.AddDays(d).Day);
//And extracting those weekdays we explicitly wanted to return
var selectedDatesQry = from d in allDatesQry
where daysToCheck.Contains(d.DayOfWeek)
select d;
//Copying the IEnumerable to a List
selectedDates = selectedDatesQry.ToList();
}
catch (Exception ex)
{
//Log error
//...
//And re-throw
throw;
}
return selectedDates;
}
Add the smallest possible number to make the first day a Monday. Subtract the smallest possible number to make the last day a Monday. Calculate the difference in days and divide by 7.
Convert the dates to Julian Day Number, then do a little bit of math. Since Mondays are zero mod 7, you could do the calculation like this:
JD1=JulianDayOf(the_first_date)
JD2=JulianDayOf(the_second_date)
Round JD1 up to nearest multiple of 7
Round JD2 up to nearest multiple of 7
d = JD2-JD1
nMondays = (JD2-JD1+7)/7 # integer divide
I have had the same need today. I started with the cjm function since I don't understand the JonB function and since the Cyberherbalist function is not linear.
I had have to correct
DifferenceInDays(Start, End) / 7 // Integer division discarding remainder
+ 1 if DayOfWeek(Start) <= DayImLookingFor
+ 1 if DayOfWeek(End) >= DayImLookingFor
- 1
to
DifferenceInDays(Start, End) / 7 // Integer division discarding remainder
+ 1 if DayImLookingFor is between Start.Day and End.Day
With the between function that return true if, starting from the start day, we meet first the dayImLookingFor before the endDay.
I have done the between function by computing the number of day from startDay to the other two days:
private int CountDays(DateTime start, DateTime end, DayOfWeek selectedDay)
{
if (start.Date > end.Date)
{
return 0;
}
int totalDays = (int)end.Date.Subtract(start.Date).TotalDays;
DayOfWeek startDay = start.DayOfWeek;
DayOfWeek endDay = end.DayOfWeek;
///look if endDay appears before or after the selectedDay when we start from startDay.
int startToEnd = (int)endDay - (int)startDay;
if (startToEnd < 0)
{
startToEnd += 7;
}
int startToSelected = (int)selectedDay - (int)startDay;
if (startToSelected < 0)
{
startToSelected += 7;
}
bool isSelectedBetweenStartAndEnd = startToEnd >= startToSelected;
if (isSelectedBetweenStartAndEnd)
{
return totalDays / 7 + 1;
}
else
{
return totalDays / 7;
}
}
This will return a collection of integers showing how many times each day of the week occurs within a date range
int[] CountDays(DateTime firstDate, DateTime lastDate)
{
var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1;
var weeks = (int)Math.Floor(totalDays / 7);
var result = Enumerable.Repeat<int>(weeks, 7).ToArray();
if (totalDays % 7 != 0)
{
int firstDayOfWeek = (int)firstDate.DayOfWeek;
int lastDayOfWeek = (int)lastDate.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
for (int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek++)
result[dayOfWeek % 7]++;
}
return result;
}
Or a slight variation which lets you do FirstDate.TotalDaysOfWeeks(SecondDate) and returns a Dictionary
public static Dictionary<DayOfWeek, int> TotalDaysOfWeeks(this DateTime firstDate, DateTime lastDate)
{
var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1;
var weeks = (int)Math.Floor(totalDays / 7);
var resultArray = Enumerable.Repeat<int>(weeks, 7).ToArray();
if (totalDays % 7 != 0)
{
int firstDayOfWeek = (int)firstDate.DayOfWeek;
int lastDayOfWeek = (int)lastDate.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
for (int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek++)
resultArray[dayOfWeek % 7]++;
}
var result = new Dictionary<DayOfWeek, int>();
for (int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++)
result[(DayOfWeek)dayOfWeek] = resultArray[dayOfWeek];
return result;
}
A bit Modified Code is here works and Tested by me
private int CountDays(DayOfWeek day, DateTime startDate, DateTime endDate)
{
int dayCount = 0;
for (DateTime dt = startDate; dt < endDate; dt = dt.AddDays(1.0))
{
if (dt.DayOfWeek == day)
{
dayCount++;
}
}
return dayCount;
}
Example:
int Days = CountDays(DayOfWeek.Friday, Convert.ToDateTime("2019-07-04"),
Convert.ToDateTime("2019-07-27")).ToString();
I had a similar problem for a report. I needed the number of workdays between two dates.
I could have cycled through the dates and counted but my discrete math training wouldn't let me. Here is a function I wrote in VBA to get the number of workdays between two dates. I'm sure .net has a similar WeekDay function.
1
2 ' WorkDays
3 ' returns the number of working days between two dates
4 Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long
5
6 Dim dtFirstSunday As Date
7 Dim dtLastSaturday As Date
8 Dim lngWorkDays As Long
9
10 ' get first sunday in range
11 dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)
12
13 ' get last saturday in range
14 dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)
15
16 ' get work days between first sunday and last saturday
17 lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5
18
19 ' if first sunday is not begin date
20 If dtFirstSunday <> dtBegin Then
21
22 ' assume first sunday is after begin date
23 ' add workdays from begin date to first sunday
24 lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))
25
26 End If
27
28 ' if last saturday is not end date
29 If dtLastSaturday <> dtEnd Then
30
31 ' assume last saturday is before end date
32 ' add workdays from last saturday to end date
33 lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)
34
35 End If
36
37 ' return working days
38 WorkDays = lngWorkDays
39
40 End Function
private System.Int32 CountDaysOfWeek(System.DayOfWeek dayOfWeek, System.DateTime date1, System.DateTime date2)
{
System.DateTime EndDate;
System.DateTime StartDate;
if (date1 > date2)
{
StartDate = date2;
EndDate = date1;
}
else
{
StartDate = date1;
EndDate = date2;
}
while (StartDate.DayOfWeek != dayOfWeek)
StartDate = StartDate.AddDays(1);
return EndDate.Subtract(StartDate).Days / 7 + 1;
}
Four years later, I thought I'd run a test:
[TestMethod]
public void ShouldFindFridaysInTimeSpan()
{
//reference: http://stackoverflow.com/questions/248273/count-number-of-mondays-in-a-given-date-range
var spanOfSixtyDays = new TimeSpan(60, 0, 0, 0);
var setOfDates = new List<DateTime>(spanOfSixtyDays.Days);
var now = DateTime.Now;
for(int i = 0; i < spanOfSixtyDays.Days; i++)
{
setOfDates.Add(now.AddDays(i));
}
Assert.IsTrue(setOfDates.Count == 60,
"The expected number of days is not here.");
var fridays = setOfDates.Where(i => i.DayOfWeek == DayOfWeek.Friday);
Assert.IsTrue(fridays.Count() > 0,
"The expected Friday days are not here.");
Assert.IsTrue(fridays.First() == setOfDates.First(i => i.DayOfWeek == DayOfWeek.Friday),
"The expected first Friday day is not here.");
Assert.IsTrue(fridays.Last() == setOfDates.Last(i => i.DayOfWeek == DayOfWeek.Friday),
"The expected last Friday day is not here.");
}
My use of TimeSpan is a bit of overkill---actually I wanted to query TimeSpan directly.

Categories

Resources