Ruby:如何正确计算日期差异?

时间:2021-06-15 01:53:31

I began with (date2 - date1).round, now this should have work.

我从(date2 - date1).round开始,现在这应该有效。

Problem :

06 Jan,2013 to 05 Feb,2013 => 30

2013年1月6日至2013年2月5日=> 30

06 Feb,2013 to 05 Mar,2013 => 27

2013年2月6日至2013年3月5日=> 27

06 Mar,2013 to 05 Apr,2013 => 30

2013年3月6日至2013年4月5日=> 30

06 Apr,2013 to 27 Apr,2013 => 21

2013年4月6日至2013年4月27日=> 21

(Date.strptime('05 Feb,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round
(Date.strptime('05 Mar,2013', '%d %b, %Y') - Date.strptime('06 Feb,2013', '%d %b,%Y')).round
(Date.strptime('05 Apr,2013', '%d %b, %Y') - Date.strptime('06 Mar,2013', '%d %b,%Y')).round
(Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Apr,2013', '%d %b,%Y')).round

Therefore,

    Total = 108 days [ 30 + 27 + 30 + 21 ]

But when I try to calculate it in 1 go as :

但是,当我尝试在1中计算它时,请执行以下操作:

(Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round

This gives :

这给出了:

Days = 111 days

Now, 108 days != 111 days

现在,108天!= 111天

What am I doing wrong ?

我究竟做错了什么 ?

2 个解决方案

#1


3  

You're missing one day between the intervals in your first code.

您在第一个代码中的间隔之间错过了一天。

I increased the end date of first 3 dates, so it matches the start of the next.

我增加了前3个日期的结束日期,因此它与下一个日期的开始相匹配。

p (Date.strptime('06 Feb,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round
p (Date.strptime('06 Mar,2013', '%d %b, %Y') - Date.strptime('06 Feb,2013', '%d %b,%Y')).round
p (Date.strptime('06 Apr,2013', '%d %b, %Y') - Date.strptime('06 Mar,2013', '%d %b,%Y')).round
p (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Apr,2013', '%d %b,%Y')).round

Output:

31
28
31
21

Sum = 111

总和= 111

#2


0  

Thans, @Dogbert. You are right I am missing it by 1

Thans,@ Dogbert。你是对的我错过了1

Although, as suggested I am missing by 1 day in all calculations.. i.e.

虽然如所建议我在所有计算中缺少1天..即

Neither of 108 or 111 are correct - Instead both are wrong

108或111都不正确 - 相反,两者都是错误的

Since, its calculating the difference between open range (d1, d2) instead of [d1, d2)

因为,它计算开放范围(d1,d2)而不是[d1,d2]之间的差异

Therefore, I should add to all of them :

因此,我应该补充所有这些:

1 + (Date.strptime('05 Feb,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round
1 + (Date.strptime('05 Mar,2013', '%d %b, %Y') - Date.strptime('06 Feb,2013', '%d %b,%Y')).round
1 + (Date.strptime('05 Apr,2013', '%d %b, %Y') - Date.strptime('06 Mar,2013', '%d %b,%Y')).round
1 + (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Apr,2013', '%d %b,%Y')).round

Therefore, total = 112 [ This time ]

因此,总数= 112 [此时间]

Similarly, I should add 1 to to full-range :

同样,我应该将1添加到全范围:

1 + (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round

This sums to 1 + 111 = 112 [ Single total ]

总和为1 + 111 = 112 [单个总数]

Therefore, 112 == 112

因此,112 == 112

#1


3  

You're missing one day between the intervals in your first code.

您在第一个代码中的间隔之间错过了一天。

I increased the end date of first 3 dates, so it matches the start of the next.

我增加了前3个日期的结束日期,因此它与下一个日期的开始相匹配。

p (Date.strptime('06 Feb,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round
p (Date.strptime('06 Mar,2013', '%d %b, %Y') - Date.strptime('06 Feb,2013', '%d %b,%Y')).round
p (Date.strptime('06 Apr,2013', '%d %b, %Y') - Date.strptime('06 Mar,2013', '%d %b,%Y')).round
p (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Apr,2013', '%d %b,%Y')).round

Output:

31
28
31
21

Sum = 111

总和= 111

#2


0  

Thans, @Dogbert. You are right I am missing it by 1

Thans,@ Dogbert。你是对的我错过了1

Although, as suggested I am missing by 1 day in all calculations.. i.e.

虽然如所建议我在所有计算中缺少1天..即

Neither of 108 or 111 are correct - Instead both are wrong

108或111都不正确 - 相反,两者都是错误的

Since, its calculating the difference between open range (d1, d2) instead of [d1, d2)

因为,它计算开放范围(d1,d2)而不是[d1,d2]之间的差异

Therefore, I should add to all of them :

因此,我应该补充所有这些:

1 + (Date.strptime('05 Feb,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round
1 + (Date.strptime('05 Mar,2013', '%d %b, %Y') - Date.strptime('06 Feb,2013', '%d %b,%Y')).round
1 + (Date.strptime('05 Apr,2013', '%d %b, %Y') - Date.strptime('06 Mar,2013', '%d %b,%Y')).round
1 + (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Apr,2013', '%d %b,%Y')).round

Therefore, total = 112 [ This time ]

因此,总数= 112 [此时间]

Similarly, I should add 1 to to full-range :

同样,我应该将1添加到全范围:

1 + (Date.strptime('27 Apr,2013', '%d %b, %Y') - Date.strptime('06 Jan,2013', '%d %b,%Y')).round

This sums to 1 + 111 = 112 [ Single total ]

总和为1 + 111 = 112 [单个总数]

Therefore, 112 == 112

因此,112 == 112