开发者

Creating a week generator showing Week no, 1st day of week, last day of week of given date

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开发者_StackOverflow社区} 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.


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;
    }
}


Not my best work, but it gets the job done.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var weekStart = DateTime.Now;
            DateTime nextWeek;

            for (int i = 0; i < 10; i++)
            {
                var weekEnd = weekStart.AddDays(6);
                if (weekEnd.Month != weekStart.Month)
                {
                    weekEnd = new DateTime(weekStart.Year, weekStart.Month, 1).AddMonths(1).AddDays(-1);
                    nextWeek = weekEnd.AddDays(1);
                }
                else
                {
                    nextWeek = weekStart.AddDays(7);
                }

                Console.WriteLine("Week {0}: F: {1} L: {2}", (weekStart.Day / 7) + 1, weekStart.ToShortDateString(), weekEnd.ToShortDateString());

                weekStart = nextWeek;
            }

            Console.ReadKey();
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜