C#计算日期间隔(小数)

参考内容

TimeSpan 结构
在 DateTime、DateOnly、DateTimeOffset、TimeSpan、TimeOnly 和 TimeZoneInfo 之间进行选择
TimeZoneInfo 类
DateTimeOffset 结构
C#日期间隔:如何计算两个日期相差几年几个月?
C#中两个日期之间的间隔天数
Calculate Year, Month and Day between two Dates in C# [duplicate]
Calculate Years, Months, weeks and Days
Difference in months between two dates

代码

大概思路是掐成三段,分别计算在相加。天到小时,周、月、年到天

点击查看代码
        public static double DiffDay(this DateTime date1,DateTime date2)
        {
            var timespan = date1 - date2;
            var days = timespan.TotalDays;

            return Math.Abs(days);
        }
		
	public static double DiffWeek1(this DateTime date1,DateTime date2)
        {
            var timespan = date1 - date2;
            var week = timespan.Days / 7d;

            return Math.Abs(week);
        }

        public static double DiffWeek(this DateTime date1,DateTime date2)
        {
            if (date2 > date1)
            {
                var date3 = date1;
                date1 = date2;
                date2 = date3;
            }
            var fromdate = date2;
            var todate = date1;

            var weekDays = 7;

            var fromdateDayOfWeek = fromdate.DayOfWeek == DayOfWeek.Sunday ? weekDays : (double)fromdate.DayOfWeek;
            var todateDayOfWeek = todate.DayOfWeek == DayOfWeek.Sunday ? weekDays : (double)todate.DayOfWeek;

            var fromdateDayInWeekRate = (weekDays - fromdateDayOfWeek + 1) / weekDays;
            var todateDayInWeekRate = todateDayOfWeek / weekDays;

            var fromdate1 = fromdate.AddDays(weekDays - fromdateDayOfWeek + 1);
            var todate1 = todate.AddDays(-(todateDayOfWeek - 1));

            var diffWeek1 = (todate1 - fromdate1).Days / weekDays;

            var diffWeek = diffWeek1 + fromdateDayInWeekRate + todateDayInWeekRate;

            return diffWeek;
        }

        public static double DiffMonth(this DateTime date1,DateTime date2)
        {
            if (date2 > date1)
            {
                var date3 = date1;
                date1 = date2;
                date2 = date3;
            }
            var fromdate = date2;
            var todate = date1;


            var fromdateDayInMonth = (double)DateTime.DaysInMonth(fromdate.Year, fromdate.Month);
            var todateDayInMonth = (double)DateTime.DaysInMonth(todate.Year, todate.Month);

            var fromdateDayInMonthRate = (fromdateDayInMonth - fromdate.Day + 1) / fromdateDayInMonth;
            var todateDayInMonthRate = todate.Day / todateDayInMonth;

            var fromdate1 = fromdate.AddDays(fromdateDayInMonth - fromdate.Day + 1);
            var todate1 = todate.AddDays(-(todate.Day - 1));

            var diffMonth1 = (todate1.Year - fromdate1.Year) * 12 + todate1.Month - fromdate1.Month;

            var diffMonth = diffMonth1 + fromdateDayInMonthRate + todateDayInMonthRate;

            return diffMonth;
        }

        public static double DiffYear(this DateTime date1,DateTime date2)
        {
            if (date2 > date1)
            {
                var date3 = date1;
                date1 = date2;
                date2 = date3;
            }
            var fromdate = date2;
            var todate = date1;

            


            var fromdateDayInYear = (double)(new DateTime(fromdate.Year+1,1,1).AddDays(-1).DayOfYear);
            var todateDayInYear = (double)(new DateTime(todate.Year + 1, 1, 1).AddDays(-1).DayOfYear);

            var fromdateDayInYearRate = (fromdateDayInYear - fromdate.DayOfYear + 1) / fromdateDayInYear;
            var todateDayInYearRate = todate.DayOfYear / todateDayInYear;

            var fromdate1 = fromdate.AddDays(fromdateDayInYear - fromdate.DayOfYear + 1);
            var todate1 = todate.AddDays(-(todate.DayOfYear - 1));

            var diffYear1 = todate1.Year - fromdate1.Year;

            var diffYear = diffYear1 + fromdateDayInYearRate + todateDayInYearRate;

            return diffYear;
        }

posted on 2023-04-27 15:03  海边灯火  阅读(123)  评论(0编辑  收藏  举报

导航