C# 从日期中减去时间跨度

C# 从日期中减去时间跨度,c#,.net,datetime,timespan,C#,.net,Datetime,Timespan,我想从一个日期时间对象中减去一个时间跨度,使用30天的月份,忽略闰年等 Date is 1983/5/1 13:0:0 (y/m/d-h:m:s) Time span is 2/4/28-2:51:0 (y/m/d-h:m:s) 我可以使用DateTime和TimeSpan对象来实现这一点,将时间跨度的年和月转换为天(假设一个月30天,一年364天) 有了这个,我得到了结果: {12/4/1978 11:09:00 PM} 上述答案显然没有忽略我想要忽略的因素,并给了我一个准确的答案。但在这

我想从一个日期时间对象中减去一个时间跨度,使用30天的月份,忽略闰年等

Date is 1983/5/1 13:0:0 (y/m/d-h:m:s)
Time span is 2/4/28-2:51:0 (y/m/d-h:m:s)
我可以使用DateTime和TimeSpan对象来实现这一点,将时间跨度的年和月转换为天(假设一个月30天,一年364天)

有了这个,我得到了结果:

{12/4/1978 11:09:00 PM}
上述答案显然没有忽略我想要忽略的因素,并给了我一个准确的答案。但在这种情况下,这不是我想要的,所以我写了下面的代码

public static CustomDateTime operator -(CustomDateTime DT1,CustomDateTime DT2)
{
    CustomDateTime retVal = new CustomDateTime();
    try
    {
        const int daysPerYear = 364.25;
        const int monthsPerYear = 12;
        const int daysPerMonth = 30;
        const int hoursPerDay = 24;
        const int minutesPerHour = 60;

        retVal.Minute = DT1.Minute - DT2.Minute;
        if (retVal.Minute < 0)
        {
            retVal.Minute += minutesPerHour;
            DT1.Hour -= 1;
        }
        retVal.Hour = DT1.Hour - DT2.Hour;
        if (retVal.Hour < 0)
        {
            retVal.Hour += hoursPerDay;
            DT1.Day -= 1;
        }
        retVal.Day = DT1.Day - DT2.Day;
        if (retVal.Day < 0)
        {
            retVal.Day += daysPerMonth;
            DT1.Month -= 1;
        }
        retVal.Month = DT1.Month - DT2.Month;
        if (retVal.Month < 0)
        {
            retVal.Month += monthsPerYear;
            DT1.Year -= 1;
        }
        retVal.Year = DT1.Year - DT2.Year;                
    }
    catch (Exception ex) { }
    return retVal;
}
这是非常接近我所追求的,除了我不应该得到0的月份和年份应该是1980年。感谢您的任何帮助



只是为了把事情弄清楚;在这种情况下,我必须使用30天一个月,而忽略闰年、不同的月数等等。我知道这是一件奇怪的事情。因此,与管理类给出的确切答案相反,我基本上是在寻找一个“错误答案”。

如果你将一个月的时间估计为30天,那么你的数学当然会被取消。当你从1981年5月1日减去878天时,.Net给出的是精确的差异,而不是估计值,如果有闰年的话,这个差异就是闰年的原因。错误不在减法(…)中,而是在您自己的“手动”计算中

DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0);
TimeSpan t = new TimeSpan(878, 13, 51, 0);

dt.Ticks
    624931668000000000

t.Ticks
    759090600000000

dt.Ticks - t.Ticks
    624172577400000000

new DateTime(dt2)
    {12/4/1978 11:09:00 PM}
    Date: {12/4/1978 12:00:00 AM}
    Day: 4
    DayOfWeek: Monday
    DayOfYear: 338
    Hour: 23
    Kind: Unspecified
    Millisecond: 0
    Minute: 9
    Month: 12
    Second: 0
    Ticks: 624172577400000000
    TimeOfDay: {23:09:00}
    Year: 1978
这些是自新纪元以来的总节拍数。做这个计算,然后转换回datetime

另外:纠正你的数学错误。878天是2年148天。1981年5月1日是一年中的121天,所以减去120就得到1979年1月1日。剩下28天。从1978年底开始倒数,你会非常接近.Net的答案。你自己的答案根本不靠谱

根据反馈进行编辑

// zh-Hans is a chinese culture
CultureInfo ci = CultureInfo.GetCultureInfo("zh-Hans");
DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);
TimeSpan t = new TimeSpan(878, 13, 51, 0);
请注意,您仍然在减去878天。在这种情况下,根据儒略历法,一个月的长度是无关紧要的。您可能需要为您的特定日历找到正确的区域性代码,然后再试一次然而,有了这个日历,我仍然得到了上面相同的答案

除此之外,我还不知道如何做数学题。如果你能提供一个链接,说明你是如何手工操作的,我可以帮你编写代码

编辑2

我现在明白了。试试这个:

DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);

int years = 878 / 365;
int remainingDays = 878 % 365;
int months = remainingDays / 30;
remainingDays = remainingDays % 30;
TimeSpan t = new TimeSpan(years * 365 + months * 30 + remainingDays);
DateTime newdate = dt.Subtract(t);

如果你把一个月的时间估计为30天,你的数学当然会被取消。当你从1981年5月1日减去878天时,.Net给出的是精确的差异,而不是估计值,如果有闰年的话,这个差异就是闰年的原因。错误不在减法(…)中,而是在您自己的“手动”计算中

DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0);
TimeSpan t = new TimeSpan(878, 13, 51, 0);

dt.Ticks
    624931668000000000

t.Ticks
    759090600000000

dt.Ticks - t.Ticks
    624172577400000000

new DateTime(dt2)
    {12/4/1978 11:09:00 PM}
    Date: {12/4/1978 12:00:00 AM}
    Day: 4
    DayOfWeek: Monday
    DayOfYear: 338
    Hour: 23
    Kind: Unspecified
    Millisecond: 0
    Minute: 9
    Month: 12
    Second: 0
    Ticks: 624172577400000000
    TimeOfDay: {23:09:00}
    Year: 1978
这些是自新纪元以来的总节拍数。做这个计算,然后转换回datetime

另外:纠正你的数学错误。878天是2年148天。1981年5月1日是一年中的121天,所以减去120就得到1979年1月1日。剩下28天。从1978年底开始倒数,你会非常接近.Net的答案。你自己的答案根本不靠谱

根据反馈进行编辑

// zh-Hans is a chinese culture
CultureInfo ci = CultureInfo.GetCultureInfo("zh-Hans");
DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);
TimeSpan t = new TimeSpan(878, 13, 51, 0);
请注意,您仍然在减去878天。在这种情况下,根据儒略历法,一个月的长度是无关紧要的。您可能需要为您的特定日历找到正确的区域性代码,然后再试一次然而,有了这个日历,我仍然得到了上面相同的答案

除此之外,我还不知道如何做数学题。如果你能提供一个链接,说明你是如何手工操作的,我可以帮你编写代码

编辑2

我现在明白了。试试这个:

DateTime dt = new DateTime(1981, 5, 1, 13, 0, 0, ci.Calendar);

int years = 878 / 365;
int remainingDays = 878 % 365;
int months = remainingDays / 30;
remainingDays = remainingDays % 30;
TimeSpan t = new TimeSpan(years * 365 + months * 30 + remainingDays);
DateTime newdate = dt.Subtract(t);

你不能假设一个月30天。您指定要减去878天。托管类(我假设你说的是本地托管类)的设计考虑了闰年、不同月数等因素


使用托管类不会给您一个月的0。

您不能假设一个月30天。您指定要减去878天。托管类(我假设你说的是本地托管类)的设计考虑了闰年、不同月数等因素


使用托管类在一个月内不会给你0分。

你说的“如果我手动执行此操作”是什么意思?我相信它的意思是“手动,在纸上”。我不知道他所说的“本土阶级”是什么意思。是的。从分钟数(m2)中减去分钟数(m1)。如果m1