Count number of days between two dates

前端 未结 10 740
暗喜
暗喜 2020-12-12 23:26

How do I count the number of days between these two dates?

start_date = Date.parse \"2012-03-02 14:46:21 +0100\"
end_date =  Date.parse \"2012-04-02 14:46:21         


        
10条回答
  •  醉酒成梦
    2020-12-12 23:38

    to get the number of days in a time range (just a count of all days)

    (start_date..end_date).count
    (start_date..end_date).to_a.size
    #=> 32
    

    to get the number of days between 2 dates

    (start_date...end_date).count
    (start_date...end_date).to_a.size
    #=> 31
    

提交回复
热议问题