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.

thanks in advance

Recommended Answers

All 2 Replies

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)
            {
                start = start.AddDays(1);
                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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.