2014-10-26 93 views

回答

43

以下是一些有用的方法來實現這一目標:

datetime = DateTime.now => Sun, 26 Oct 2014 21:00:00 

datetime.today? # => true 
datetime.to_date.past? # => false (only based on date) 
datetime.to_date.future? # => false (only based on date) 

datetime.to_date == Date.tomorrow # => false 
datetime.to_date == Date.yesterday # => false 
2

喜歡的東西...

datetime = Time.now.to_datetime 
=> Sun, 26 Oct 2014 16:24:55 -0600 

datetime >= Date.today 
=> true 

datetime < Date.tomorrow 
=> true 

datetime += 1.day 
=> Mon, 27 Oct 2014 16:25:12 -0600 

datetime >= Date.today 
=> true 

datetime >= Date.tomorrow 
=> true 

datetime < (Date.tomorrow + 1.day) 
=> false 

+0

我雖然你釘它,但.. HM。總之,我喜歡以下是真實的,但它不是..'DateTime.current == Date.current' – Numbers 2014-10-26 22:39:32

+1

'DateTime.current.to_date == Date.current'? – 2014-10-26 22:45:39

+0

當然! :)(只需刪除DateTime類中的點..) – Numbers 2014-10-26 22:47:25

相關問題