计算2个日期之间的包含周数

时间:2016-09-30 09:44:40

标签: c#

我需要获得2个日期之间的周数 对我来说,一周是星期一到星期日 因此,如果第一个日期是星期六,那么应该包括本周 如果第二个日期是星期一,那么应该包括本周。

最有效的方法是什么?

示例:

startdate   enddate      nbr of weeks
17/09/2016  26/09/2016   3 weeks
17/09/2016  25/09/2016   2 weeks
19/09/2016  26/09/2016   2 weeks
12/09/2016  25/09/2016   2 weeks

我找到了很多答案,比如这个例如how to calculate number of weeks given 2 dates?,但他们最终都将天数除以7而这并没有给出我需要的结果。

3 个答案:

答案 0 :(得分:7)

最简单的方法可能就是写一个方法来开始一周的开始。然后你可以从另一个日期中减去一个日期,将天数除以7并加1(以使其包含在内)。

就我所有人而言,我个人使用Noda Time,但使用DateTime

// Always uses Monday-to-Sunday weeks
public static DateTime GetStartOfWeek(DateTime input)
{
    // Using +6 here leaves Monday as 0, Tuesday as 1 etc.
    int dayOfWeek = (((int) input.DayOfWeek) + 6) % 7;
    return input.Date.AddDays(-dayOfWeek);
}

public static int GetWeeks(DateTime start, DateTime end)
{
    start = GetStartOfWeek(start);
    end = GetStartOfWeek(end);
    int days = (int) (end - start).TotalDays;
    return (days / 7) + 1; // Adding 1 to be inclusive
}

完整示例:

using System;

class Program
{
    static void Main (string[] args)
    {
        ShowWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 26));
        ShowWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 25));
        ShowWeeks(new DateTime(2016, 9, 19), new DateTime(2016, 9, 26));
        ShowWeeks(new DateTime(2016, 9, 12), new DateTime(2016, 9, 25));
    }

    static void ShowWeeks(DateTime start, DateTime end)
    {
        int weeks = GetWeeks(start, end);
        Console.WriteLine($"{start:d} {end:d} {weeks}");
    }

    // Always uses Monday-to-Sunday weeks
    public static DateTime GetStartOfWeek(DateTime input)
    {
        // Using +6 here leaves Monday as 0, Tuesday as 1 etc.
        int dayOfWeek = (((int) input.DayOfWeek) + 6) % 7;
        return input.Date.AddDays(-dayOfWeek);
    }

    public static int GetWeeks(DateTime start, DateTime end)
    {
        start = GetStartOfWeek(start);
        end = GetStartOfWeek(end);
        int days = (int) (end - start).TotalDays;
        return (days / 7) + 1; // Adding 1 to be inclusive
    }
}

输出(在我的英国语言环境中):

17/09/2016 26/09/2016 3
17/09/2016 25/09/2016 2
19/09/2016 26/09/2016 2
12/09/2016 25/09/2016 2

答案 1 :(得分:4)

有点“简化”,因为星期一只需要开始日期:

static int weeks(DateTime d1, DateTime d2) { 
    var daysSinceMonday = ((int)d1.DayOfWeek + 6) % 7;
    return ((d2 - d1).Days + daysSinceMonday) / 7 + 1; 
}

答案 2 :(得分:3)

请参阅下面的方法,我将星期几“星期几”延长至星期日,然后计算总天数/ 7

    public static void Main(string[] args)
    {
        Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 26)));
        Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 25)));
        Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 19), new DateTime(2016, 9, 26)));
        Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 12), new DateTime(2016, 9, 25)));

    }

    public static double CalculateWeeks(DateTime from, DateTime to)
    {
        if (to.DayOfWeek != DayOfWeek.Sunday)
            to = to.Add(new TimeSpan(7- (int) to.DayOfWeek, 0, 0, 0)).Date;
        return Math.Ceiling((to - from.Subtract(new TimeSpan((int)from.DayOfWeek - 1, 0, 0, 0)).Date).TotalDays / 7);
    }