Number of days between two Date objects (Ruby)
来源:互联网 发布:科比每个赛季的数据 编辑:程序博客网 时间:2024/05/16 19:28
How can I find the number of days between two Date objects?
irb(main):005:0> a = Date.parse("12/1/2010")=> #<Date: 4911063/2,0,2299161>irb(main):007:0> b = Date.parse("12/21/2010")=> #<Date: 4911103/2,0,2299161>irb(main):016:0> c = b.mjd - a.mjd=> 20
This uses a Modified Julian Day Number.
-------------------------------------------------------------------------------------------------------------------
This may have changed in Ruby 2.0
When I do this I get a fraction. For example on the console (either irb or rails c)
2.0.0-p195 :005 > require 'date' => true 2.0.0-p195 :006 > a_date = Date.parse("25/12/2013") => #<Date: 2013-12-25 ((2456652j,0s,0n),+0s,2299161j)> 2.0.0-p195 :007 > b_date = Date.parse("10/12/2013") => #<Date: 2013-12-10 ((2456637j,0s,0n),+0s,2299161j)> 2.0.0-p195 :008 > a_date-b_date => (15/1)
Of course, casting to an int give the expected result
2.0.0-p195 :009 > (a_date-b_date).to_i => 15
This also works for DateTime objects, but you have to take into consideration seconds, such as this example
2.0.0-p195 :017 > a_date_time = DateTime.now => #<DateTime: 2013-12-31T12:23:03-08:00 ((2456658j,73383s,725757000n),-28800s,2299161j)> 2.0.0-p195 :018 > b_date_time = DateTime.now-20 => #<DateTime: 2013-12-11T12:23:06-08:00 ((2456638j,73386s,69998000n),-28800s,2299161j)> 2.0.0-p195 :019 > a_date_time - b_date_time => (1727997655759/86400000000) 2.0.0-p195 :020 > (a_date_time - b_date_time).to_i => 19 2.0.0-p195 :021 > c_date_time = a_date_time-20 => #<DateTime: 2013-12-11T12:23:03-08:00 ((2456638j,73383s,725757000n),-28800s,2299161j)> 2.0.0-p195 :022 > a_date_time - c_date_time => (20/1) 2.0.0-p195 :023 > (a_date_time - c_date_time).to_i => 20
----------------------------------------------------------------------------------------------------------------------------------------------------In Ruby 2.1.3 things have changed:
> endDate = Date.new(2014, 1, 2) => #<Date: 2014-01-02 ((2456660j,0s,0n),+0s,2299161j)> > beginDate = Date.new(2014, 1, 1) => #<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)> > days = endDate - beginDate => (1/1) > days.class => Rational > days.to_i => 1
0 0
- Number of days between two Date objects (Ruby)
- Calculate difference between two dates (number of days) in C#?
- Add n number of working days to date
- Log of these two days
- 88.You need to calculate the number of days from 1st January 2007 till date.
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- Select arrays between date ranges with Ruby
- Find the difference between two days in days, months and years
- Python.Days Between Dates
- count the number of objects--物体计数
- Sum of two integer number
- Python checkio Days Between解决方案
- two type of parsing date on iPhone
- Communicate between objects
- leave after two days
- Reduce a date range to an array of the last days of each month?
- Find distance between two given keys of a Binary Tree
- sum of two 20-based number
- java根据日期生成单号
- 创建一个双链表结点
- 浅入浅出之常用正则表达式
- 用JS获取地址栏参数的方法(超级简单)
- 【LeetCode】Generate Parentheses
- Number of days between two Date objects (Ruby)
- 405 错误不允许访问 MP3格式-nginx
- Sky数
- NSLog只有在debug模式下才可以看到
- JS控制在textarea中换行
- 行人检测(综述3_相关源码)
- pcl-master的编译
- Android之Toast显示不出来原因
- 时间选择器