none
DateTime类DaysTo1899字段的时间计算 RRS feed

  • 问题

  • .NET DateTime类的源代码中显示DaysTo1899字段的计算方法为:

    // Number of days from 1/1/0001 to 12/30/1899
    private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;

    可是1900年是平年有365天,加上1899年12月31日这一天也才366天,那最后减去的367天里这多出来的1天在什么地方呢?

    2015年2月12日 0:39

答案

  • 你好,

    我也没怎么了解过这个算法具体应该是什么样子的,但是从源码上来看,计算1899年不是用1900年这一年来算的吧:

    // Number of days in a non-leap year
            private const int DaysPerYear = 365;
            // Number of days in 4 years
            private const int DaysPer4Years = DaysPerYear * 4 + 1;       // 1461
            // Number of days in 100 years
            private const int DaysPer100Years = DaysPer4Years * 25 - 1;  // 36524
            // Number of days in 400 years
            private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097
        
            // Number of days from 1/1/0001 to 12/31/1600
            private const int DaysTo1601 = DaysPer400Years * 4;          // 584388
            // Number of days from 1/1/0001 to 12/30/1899
            private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
            // Number of days from 1/1/0001 to 12/31/9999
            private const int DaysTo10000 = DaysPer400Years * 25 - 366;  // 3652059
    
    按你的想法的话不是应该是用下面的算式算到1900年再减去1900年的天数么?



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月15日 3:04
    版主