如何将两周添加到 Time.now? [英] How do I add two weeks to Time.now?

查看:39
本文介绍了如何将两周添加到 Time.now?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ruby 中为当前的 Time.now 添加两周?我有一个使用 DataMapper 的小型 Sinatra 项目,在保存之前,我有一个字段填充了当前时间加上两周,但没有按需要工作.任何帮助是极大的赞赏!我收到以下错误:

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

这是模型的代码:

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

推荐答案

您在普通 Ruby 中没有这么好的帮手.您可以添加秒数:

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

这篇关于如何将两周添加到 Time.now?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆