创建一个周生成器,显示周数,周的第一天,给定日期的最后一天

时间:2011-07-04 12:43:31

标签: c# .net

我正在尝试在C#中为我的工资单程序创建一个周生成器。我创建了一个类似的,在给定的日期创建一年中的几个月。

我想在给定日期输出如下。例如。今天是2010年7月4日。

如果我选择我想要上一个日期的下一个10周,我想要打印以下内容:

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 

我希望上述内容适用于任何日期以及我想要生成的任意数周。

有4个班级:

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

我的两个问题:

  1. 在program.cs中,我知道这行不正确

    Week = (WeeksOfAMonth)givenDate.Day, 
    

    如何显示正确的一周?

  2. 无论月份是否已经结束,我的givenDate变量都会继续增加7天。我希望它跳到下个月,即使该月的最后一周少于7天。我如何实现这一目标。

  3. 任何帮助表示赞赏。对于那些在美国,独立日快乐的人。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

不是我最好的工作,但它完成了工作。

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

    }
}