C# 计算每季度的天数

C# 计算每季度的天数,c#,C#,给定一个DateTime对象,我想知道它是各自季度的哪一天 2月2日是第一季度的第33天 我可以从月份中得到季度,如下所示: intSystem.Math.Ceilingmonth/double 3 我还可以使用DateTime.DaysInMonth来确定每个季度的天数 我写了这个特别难看的方法,希望有人能给我指出一些明显的东西,我确信我错过了 public static int DayOfQuarter(DateTime dt) { var quart

给定一个DateTime对象,我想知道它是各自季度的哪一天

2月2日是第一季度的第33天

我可以从月份中得到季度,如下所示: intSystem.Math.Ceilingmonth/double 3

我还可以使用DateTime.DaysInMonth来确定每个季度的天数

我写了这个特别难看的方法,希望有人能给我指出一些明显的东西,我确信我错过了

public static int DayOfQuarter(DateTime dt)
        {
            var quarter = GetQuarter(dt);

            int[] months = new int[0];
            switch (quarter)
            {
                case 1:
                    months = new[] { 1, 2, 3 };
                    break;
                case 2:
                    months = new[] { 4, 5, 6 };
                    break;
                case 3:
                    months = new[] { 7, 8, 9 };
                    break;
                case 4:
                    months = new[] { 10, 11, 12 };
                    break;
            }

            var idx = -1;
            for (var i = 0; i < months.Length; i++)
            {
                if (months[i] == dt.Month)
                {
                    idx = i;
                }
            }

            if (idx == 0)
            {
                return dt.Day;
            }

            if (idx == 1)
            {
                return DateTime.DaysInMonth(dt.Year, dt.Month - 1) + dt.Day;
            }

            return DateTime.DaysInMonth(dt.Year, dt.Month - 2) + DateTime.DaysInMonth(dt.Year, dt.Month - 1) + dt.Day;
        }

是的,我想我们可以使用DayOfYear属性简化这个过程。你只需要计算出季度的开始日期,然后你就可以从指定的日期开始计算该日期的年份

public static int DayOfQuarter(DateTime dt)
{
    int zeroBasedQuarter = (dt.Month - 1) / 3;
    DateTime startOfQuarter = new DateTime(dt.Year, zeroBasedQuarter * 3 + 1, 1);
    return dt.DayOfYear - startOfQuarter.DayOfYear + 1;
}
下面是一个简短但完整的测试应用程序:

using System;

public class Program
{
    static void Main()
    {
        var dates = new[]
        {
            new DateTime(2000, 1, 1),
            new DateTime(2001, 1, 1),
            new DateTime(2000, 3, 1),
            new DateTime(2001, 3, 1),
            new DateTime(2000, 4, 1),
            new DateTime(2001, 4, 1),
            new DateTime(2000, 5, 1),
            new DateTime(2001, 5, 1),
            new DateTime(2000, 12, 31),
            new DateTime(2001, 12, 31),
        };
        foreach (var date in dates)
        {
            int dayOfQuarter = DayOfQuarter(date);
            Console.WriteLine($"{date:yyyy-MM-dd}: {dayOfQuarter}");
        }
    }

    public static int DayOfQuarter(DateTime dt)
    {
        int zeroBasedQuarter = (dt.Month - 1) / 3;
        DateTime startOfQuarter = new DateTime(dt.Year, zeroBasedQuarter * 3 + 1, 1);
        return dt.DayOfYear - startOfQuarter.DayOfYear + 1;
    }
}
输出:

2000-01-01: 1
2001-01-01: 1
2000-03-01: 61
2001-03-01: 60
2000-04-01: 1
2001-04-01: 1
2000-05-01: 31
2001-05-01: 31
2000-12-31: 92
2001-12-31: 92
注意第三行和第四行,这表明它在闰年中做了正确的事情,其中3月1日的季度日比闰年大一天

我还建议在GetQuarter方法中避免使用浮点-这是不必要的:

public static int GetQuarter(DateTime dt) => (dt.Month - 1) / 3 + 1;

是的,我想我们可以使用DayOfYear属性简化这个过程。你只需要计算出季度的开始日期,然后你就可以从指定的日期开始计算该日期的年份

public static int DayOfQuarter(DateTime dt)
{
    int zeroBasedQuarter = (dt.Month - 1) / 3;
    DateTime startOfQuarter = new DateTime(dt.Year, zeroBasedQuarter * 3 + 1, 1);
    return dt.DayOfYear - startOfQuarter.DayOfYear + 1;
}
下面是一个简短但完整的测试应用程序:

using System;

public class Program
{
    static void Main()
    {
        var dates = new[]
        {
            new DateTime(2000, 1, 1),
            new DateTime(2001, 1, 1),
            new DateTime(2000, 3, 1),
            new DateTime(2001, 3, 1),
            new DateTime(2000, 4, 1),
            new DateTime(2001, 4, 1),
            new DateTime(2000, 5, 1),
            new DateTime(2001, 5, 1),
            new DateTime(2000, 12, 31),
            new DateTime(2001, 12, 31),
        };
        foreach (var date in dates)
        {
            int dayOfQuarter = DayOfQuarter(date);
            Console.WriteLine($"{date:yyyy-MM-dd}: {dayOfQuarter}");
        }
    }

    public static int DayOfQuarter(DateTime dt)
    {
        int zeroBasedQuarter = (dt.Month - 1) / 3;
        DateTime startOfQuarter = new DateTime(dt.Year, zeroBasedQuarter * 3 + 1, 1);
        return dt.DayOfYear - startOfQuarter.DayOfYear + 1;
    }
}
输出:

2000-01-01: 1
2001-01-01: 1
2000-03-01: 61
2001-03-01: 60
2000-04-01: 1
2001-04-01: 1
2000-05-01: 31
2001-05-01: 31
2000-12-31: 92
2001-12-31: 92
注意第三行和第四行,这表明它在闰年中做了正确的事情,其中3月1日的季度日比闰年大一天

我还建议在GetQuarter方法中避免使用浮点-这是不必要的:

public static int GetQuarter(DateTime dt) => (dt.Month - 1) / 3 + 1;

联合收割机和dt.DayOfYear-新日期时间dt.Year,1+3*dt.Month-1/3,1.DayOfYear联合收割机的可能副本和dt.DayOfYear-新日期时间dt.Year,1+3*dt.Month-1/3,1.DayOfYear的可能副本