开发者

How do I add two weeks to Time.now?

How can I add two weeks to the current Time.now in Ruby? I have a small Sinatra project that uses DataMapper and before saving, I have a field populated with the current time PLUS two weeks,开发者_开发问答 but is not working as needed. Any help is greatly appreciated! I get the following error:

NoMethodError at /
undefined method `weeks' for 2:Fixnum

Here is the code for the Model:

class Job
  include DataMapper::Resource

  property :id,           Serial
  property :position,     String
  property :location,     String
  property :email,        String
  property :phone,        String
  property :description,  Text
  property :expires_on,   Date
  property :status,       Boolean
  property :created_on,   DateTime
  property :updated_at,   DateTime

  before :save do
    t = Time.now
    self.expires_on = t + 2.week
    self.status = '0'
  end
end


You don't have such nice helpers in plain Ruby. You can add seconds:

Time.now + (2*7*24*60*60)

But, fortunately, there are many date helper libraries out there (or build your own ;) )


Ruby Date class has methods to add days and months in addition to seconds in Time. An example:

require 'date'
t = DateTime.now
puts t      # => 2011-05-06T11:42:26+03:00

# Add 14 days
puts t + 14 # => 2011-05-20T11:42:26+03:00

# Add 2 months
puts t >> 2 # => 2011-07-06T11:42:26+03:00

# And if needed, make Time object out of it
(t + 14).to_time   # => 2011-05-20 11:42:26 +0300


require 'rubygems'
require 'active_support/core_ext/numeric/time'
self.expires = 2.weeks.from_now


You have to use seconds to do calculation between dates, but you can use the Time class as a helper to get the seconds from the date part elements.

Time.now + 2.week.to_i

EDIT: As mentioned by @iain you will need Active Support to accomplish usign 2.week.to_i, if you can't (or don't want to) have this dependency you can always use the + operator to add seconds to a Time instance (time + numeric → time docs here)

Time.now + (60 * 60 * 24 * 7 * 2)


I think week/weeks is defined in the active support numeric extension

$ ruby -e 'p Time.now'
2011-05-05 22:27:04 -0400
$ ruby -r active_support/core_ext/numeric -e 'p Time.now + 2.weeks'
2011-05-19 22:27:07 -0400


You can use these 3 patterns

# you have NoMethod Error undefined method 
require 'active_support/all'

# Tue, 28 Nov 2017 11:46:37 +0900
Time.now + 2.weeks

# Tue, 28 Nov 2017 11:46:37 +0900
Time.now + 2.week

# Tue Nov 28 11:48:24 +0900 2017
2.weeks.from_now


<%current_time=Time.now
  current_time_s=current_time.strftime('%Y-%m-%d %H:%M:%S').to_s #show currrent date time
 current_time= Time.now + (60 * 60 * 24 * 7 * 250)
 current_time_e=current_time.strftime('%Y-%m-%d %H:%M:%S').to_s #show datetime after week
%>


I like mine too :)

def minor?(dob)
  n = DateTime.now
  a = DateTime.parse(dob)
  a >> 12*18 > n
end

Saves you the trouble of thinking about leap years and seconds. Just works out of the box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜