简体   繁体   中英

Rails 3/Ruby 1.9.2 Date.tomorrow not correct

Using Ruby 1.9.2 and Rail 3 on Mac Snow leopard

ruby-1.9.2-p290 :001 > Date.today
 => Sun, 25 Sep 2011 
ruby-1.9.2-p290 :002 > Date.tomorrow
 => Tue, 27 Sep 2011 

Is there maybe something wrong with the ruby date class or is this something to do with the way i installed ruby?

Edit:

ruby-1.9.2-p290 :039 > Date.current
 => Mon, 26 Sep 2011 
ruby-1.9.2-p290 :040 > DateTime.now
 => Sun, 25 Sep 2011 20:47:01 -0500

Ok so the rails Date class seems a little buggy. The DateTime class appears to work fine though. Thanks derp and Adam

Was looking info online and came across this:
https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday


Check the last comment:

I do agree with you that its a bit confusing that you need to use Date.current with Date.yesterday instead of Date.today, but the general rule of thumb is Rails does not change how Ruby methods work, which Date.today is. All we can do is add better documentation and make sure people are aware of the subtle difference.


In other words, use current (rails) instead of today (ruby) to avoid problems.

I am also observing this. You can try DateTime.now.tomorrow.to_date .

你也可以做Date.today + 1.day

If you're using newer version of ruby then you can use Date.current.tomorrow to print tomorrow's date.

For example : puts "Tomorrow's date is : #{Date.current.tomorrow}"

To know more about data and time please follow below links.

RubyOnRails.org and RailsGithub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM