Hi is there an accepted way to calculate the number of times a specific day would come round in a given timespan?

For example if we have 32 as a variable that represents the number of weeks I would then need to find out how many times the first of the month would come round within those 32 weeks.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jlivvers

I did some code for you. I have even put the numericUpDown control on the form to select numbe of weeks randomly. And to select the beginning of calculation, you choose if from dateTimePicker. This is the code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DateTime start = this.dateTimePicker1.Value.Date;
int weeks = Convert.ToInt32(this.numericUpDown1.Value);
if (weeks > 0)
DaysCalculations(start, weeks);
else
MessageBox.Show("Calculation cannot start. Number of the weeks to calculate is set to zero.");
}

private void DaysCalculations(DateTime start, int weeks)
{
DateTime end = start.AddDays(7 * weeks);
int NumberOfDays = 0;
string strAllDates = null;
while (start <= end)
{
if (start == new DateTime(start.Year, start.Month, 1))
{
NumberOfDays++;
strAllDates += start.ToShortDateString() + Environment.NewLine;
}
}
if (strAllDates == null)
strAllDates = "No dates";
MessageBox.Show("Between " + start.ToShortDateString() + " and the selected date: " + end.ToShortDateString() +
"\nfirst of the month comes " + NumberOfDays.ToString() + " times.\n\n" +
"The list of all 1st of the month:\n" + strAllDates);
}

}

wow thanks for that!