生成每周日期

时间:2012-02-26 08:25:39

标签: c# wpf winforms silverlight date

我确信这已经完成,所以我正在寻找一种有效的解决方案,而不是自己的定制解决方案。

鉴于2个日期,我正在尝试生成准确的每周日期(用于创建每周订单)。

编辑:我需要使用.NET标准库来执行此操作。

Example below,

Given 28/02/2012 and 6/03/2012.

so, the weekly dates generated are
- Week From(Start Monday):   Week To(End Sunday):
- 27/02/2012               - 04/03/2012
- 05/03/2012               - 11/03/2012

Another example (1 month)

Given 01/02/2012 and 29/02/2012
so, the weekly dates generated are
- Week From(Start Monday):   Week To(End Sunday):
- 30/01/2012               - 05/02/2012
- 06/02/2012               - 12/02/2012
- 13/02/2012               - 19/02/2012
- 20/02/2012               - 26/02/2012
- 27/02/2012               - 04/03/2012

我在c#中这样做。以前做过吗?介意分享解决方案?

干杯

3 个答案:

答案 0 :(得分:7)

以下是使用Noda Time的解决方案。不可否认,它需要一个<=运算符,我现在正在实施 - 但这不应该花费很长时间:)

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        ShowDates(new LocalDate(2012, 2, 28), new LocalDate(2012, 3, 6));
        ShowDates(new LocalDate(2012, 2, 1), new LocalDate(2012, 2, 29));
    }

    static void ShowDates(LocalDate start, LocalDate end)
    {
        // Previous is always strict - increment start so that
        // it *can* be the first day, then find the previous
        // Monday
        var current = start.PlusDays(1).Previous(IsoDayOfWeek.Monday);
        while (current <= end)
        {
            Console.WriteLine("{0} - {1}", current,
                              current.Next(IsoDayOfWeek.Sunday));
            current = current.PlusWeeks(1);
        }
    }
}

显然,也可以在正常的DateTime中执行此操作,但是没有“只是日期”的真实表示,这使得代码不太清晰 - 您需要自己实现Previous

编辑:例如,在这种情况下,您可以使用:

using System;

class Test
{
    static void Main()
    {
        ShowDates(new DateTime(2012, 2, 28), new DateTime(2012, 3, 6));
        ShowDates(new DateTime(2012, 2, 1), new DateTime(2012, 2, 29));
    }

    static void ShowDates(DateTime start, DateTime end)
    {
        // In DateTime, 0=Sunday
        var daysToSubtract = ((int) start.DayOfWeek + 6) % 7;
        var current = start.AddDays(-daysToSubtract);        

        while (current <= end)
        {
            Console.WriteLine("{0} - {1}", current, current.AddDays(6));
            current = current.AddDays(7);
        }
    }
}

答案 1 :(得分:0)

假设您不必弄清楚开始日期是星期一:

var slots = new List<Tuple<DateTime, DateTime>>();
DateTime start = new DateTime(2012, 2, 28);
DateTime end = new DateTime(2012, 3, 6);
for (DateTime i = start; i < end; i = i.AddDays(7))
{
    slots.Add(new Tuple<DateTime, DateTime>(i, i.AddDays(6)));
}

foreach (var slot in slots)
{
    Console.WriteLine("{0}\t{1}", slot.Item1.ToString("dd/MM/yyyy"), slot.Item2.ToString("dd/MM/yyyy"));
}

编辑:假设你必须弄明白星期一和星期日涵盖的日期范围,你可以向后移动一天,直到你遇到星期一,然后一天向前移动,直到你到达星期天。

class Program
{
    static void Main(string[] args)
    {
        var slots = new List<Tuple<DateTime, DateTime>>();
        DateTime start = FirstMonday(new DateTime(2012, 2, 28));
        DateTime end = FirstSunday(new DateTime(2012, 3, 6));
        for (DateTime i = start; i < end; i = i.AddDays(7))
        {
            slots.Add(new Tuple<DateTime, DateTime>(i, i.AddDays(6)));
        }

        foreach (var slot in slots)
        {
            Console.WriteLine("{0}\t{1}", slot.Item1.ToString("dd/MM/yyyy"), slot.Item2.ToString("dd/MM/yyyy"));
        }

        Console.ReadLine();
    }

    static DateTime FirstMonday(DateTime date)
    {
        while (date.DayOfWeek != DayOfWeek.Monday) date = date.AddDays(-1);
        return date;
    }
    static DateTime FirstSunday(DateTime date)
    {
        while (date.DayOfWeek != DayOfWeek.Sunday) date = date.AddDays(1);
        return date;
    }
}

答案 2 :(得分:0)

此解决方案允许您自定义开始和结束DayOfWeek:

<强>解决方案:

public Dictionary<DateTime, DateTime> GetWeeklyDateTimes(DateTime from, DateTime to, DayOfWeek startDay, DayOfWeek endDay)
    {
        int startEndSpan = 7 - endDay - startDay;

        // Subtract days until it falls on our desired start day
        from = from.AddDays(startDay - from.DayOfWeek);
        // Add days until it falls on our desired end day
        to = to.AddDays(to.DayOfWeek - endDay + 2);

        Dictionary<DateTime, DateTime> dateTimes = new Dictionary<DateTime, DateTime>();

        while (to.Subtract(from).Days > startEndSpan)
        {
            dateTimes.Add(from, from.AddDays(startEndSpan));

            from = from.AddDays(startEndSpan + 1);
        }

        return dateTimes;
    }

使用示例:

// DateTime(2012, 2, 1) corresponds to Year 2012, Month February, Day 1
Dictionary<DateTime, DateTime> dateTimes = GetWeeklyDateTimes(new DateTime(2012, 2, 1), new DateTime(2012, 2, 29), DayOfWeek.Monday, DayOfWeek.Sunday);

foreach (KeyValuePair<DateTime, DateTime> entry in dateTimes)
{
    Trace.WriteLine(entry.Key.ToString() + "     " + entry.Value.ToString());
}