Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

ruby - 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
question from:https://stackoverflow.com/questions/5905861/how-do-i-add-two-weeks-to-time-now

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ;) )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...