Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:28+00:00 2026-05-23T12:13:28+00:00

I am trying to create a week generator in C# for my payroll program.

  • 0

I am trying to create a week generator in C# for my payroll program. I have created a similar one that creates months of a year for a given date.

I would like my output as follows for a given date. E.G. Today is 4th July 2010.

If I select that I want the next 10 Weeks for the above date, I would like the following to print:

Week 1:   F: 04/07/2011    L: 10/07/2011
Week 2:   F: 11/07/2011    L: 17/07/2011 
Week 3:   F: 18/07/2011    L: 24/07/2011 
Week 4:   F: 25/07/2011    L: 31/07/2011 
Week 1:   F: 01/08/2011    L: 07/08/2011 
Week 2:   F: 08/08/2011    L: 14/08/2011 
Week 3:   F: 15/08/2011    L: 21/08/2011 
Week 4:   F: 22/08/2011    L: 28/08/2011 
Week 5:   F: 29/08/2011    L: 31/08/2011 
Week 1:   F: 01/09/2011    L: 07/09/2011 

I would like the above to work for any date and for whatever number of weeks that I want to generate.

There are 4 classes:

Program.cs:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var dateInfos = GetDateInfo(DateTime.Now, 6);

            foreach (var item in dateInfos)
                Console.WriteLine("W: {0} F: {1} L: {2}", item.Week.ToString(), item.FirstDayOfWeek, item.LastDayOfWeek);

            Console.ReadKey();
        }

        public static IList<DatesBag> GetDateInfo(DateTime givenDate, int numberOfWeeks)
        {
            var result = new List<DatesBag>();    
            result.Add(new DatesBag                
            { 
                Week = (WeeksOfAMonth)givenDate.Day, 
                FirstDayOfWeek = Week.GetFirstDayOfWeek(givenDate), 
                LastDayOfWeek = Week.GetLastDayOfWeek(givenDate) 
            });    
            for (int i = 1; i < numberOfWeeks; i++)
            {
                givenDate = givenDate.AddDays(7);    
                result.Add(new DatesBag { Week = (WeeksOfAMonth)givenDate.Day, FirstDayOfWeek = Week.GetFirstDayOfWeek(givenDate), LastDayOfWeek = Week.GetLastDayOfWeek(givenDate) });
            }

            return result;
        }
    }
}

DatesBag.cs:

namespace ConsoleApplication1
{
    public class DatesBag
    {
        public WeeksOfAMonth Week { get; set; }

        public DateTime FirstDayOfWeek { get; set; }

        public DateTime LastDayOfWeek { get; set; }
    }
}

WeeksOfAMonth.cs:

namespace ConsoleApplication2
{
    public enum WeeksOfAMonth
    {
        Week1 = 1,
        Week2,
        Week3,
        Week4,
        Week5        
    }
}

Week.cs

namespace Payroll.Util.Helpers
{
    public static class Week
    {
        /// <summary>
        /// Gets the first day of the week.
        /// </summary>
        /// <param name="givenDate">The given date.</param>
        /// <returns>the first day of the week</returns>
        public static DateTime GetFirstDayOfWeek(DateTime givenDate)
        {
            return new DateTime(givenDate.Year, givenDate.Month, givenDate.Day);
        }

        /// <summary>
        /// Gets the last day of week.
        /// </summary>
        /// <param name="givenDate">The given date.</param>
        /// <returns>the last day of the week</returns>
        public static DateTime GetLastDayOfWeek(DateTime givenDate)
        {
            return GetFirstDayOfWeek(givenDate).AddDays(7).Subtract(new TimeSpan(1, 0, 0, 0, 0));
        }
    }
}

My 2 problems:

  1. In program.cs, i have this line that I know is incorrect

    Week = (WeeksOfAMonth)givenDate.Day, 
    

    How do I get to show the correct week?

  2. My givenDate variable keeps adding 7 days no matter whether the month has already ended or not. i want It to skip to the next month even if the last week of the month has less than 7 days. How do I achieve this.

Any help appreciated. For those in USA, Happy Independence Day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:13:29+00:00Added an answer on May 23, 2026 at 12:13 pm
    public static IList<DatesBag> GetDateInfo(DateTime givenDate, int numberOfWeeks)
    {
        var result = new List<DatesBag>();
    
        for (int i = 0; i < numberOfWeeks; i++)
        {
            int firstWeekDay = Week.GetFirstDayOfWeek(givenDate).Day;
    
            DatesBag datesBag = new DatesBag
                                    {
                                        Week = (WeeksOfAMonth) ((firstWeekDay / 7) + ((firstWeekDay % 7 == 0) ? 0 : 1)),
                                        FirstDayOfWeek = Week.GetFirstDayOfWeek(givenDate),
                                        LastDayOfWeek = Week.GetLastDayOfWeek(givenDate)
                                        };
            result.Add(datesBag);
            givenDate = givenDate.AddDays(datesBag.WeekLength + 1);
         }
        return result;
    }
    
    
    public class DatesBag
    {
        public WeeksOfAMonth Week { get; set; }
    
        public DateTime FirstDayOfWeek { get; set; }
    
        public DateTime LastDayOfWeek { get; set; }
    
        public int WeekLength { get { return LastDayOfWeek.Day - FirstDayOfWeek.Day; } }
    }
    
    
    public static class Week
    {
        public static DateTime GetFirstDayOfWeek(DateTime givenDate)
        {
            DateTime tmp = givenDate.Date;
            while (tmp.AddDays(-1).Month == givenDate.Month && tmp.DayOfWeek > DayOfWeek.Monday)
            {
                tmp = tmp.AddDays(-1);
            }
            return tmp;
        }
    
        public static DateTime GetLastDayOfWeek(DateTime givenDate)
        {
            DateTime tmp = givenDate.Date;
            while (tmp.AddDays(1).Month == givenDate.Month && tmp.DayOfWeek != DayOfWeek.Sunday)
            {
                tmp = tmp.AddDays(1);
            }
            return tmp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a style that makes dates from the last week
I have started Gwt last week. And I was trying to create application without
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create a google annotated timeline viz. For that I need
I'm trying to create a weekly calendar that looks like this: http://dhtmlx.com/docs/products/dhtmlxScheduler/sample_basic.html How can
I have been trying to get up to speed on Named Pipes this week.
I am trying to create a menu where users can view a menu that
I'm trying to create some javascript code that will display a javascript form (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.