草庐IT

datetime2

全部标签

Ruby DateTime 范围只有 1 天的粒度

在ruby​​中,DateTime范围似乎只有1天的粒度。now=DateTime.new(2013,1,1)#=>#two_days_later=DateTime.new(2013,1,3)#=>#range=(now..two_days_later)#=>#..#tomorrow=DateTime.new(2013,1,2)#=>#range.include?(tomorrow)#=>truelater_this_morning=now+0.1#=>#range.include?later_this_morning#=>false#but.....range.max#所需的行为是它

ruby-on-rails - rails : validation fails with before_create method on datetime attribute

ApiKey.create!抛出验证错误:expires_at不能为空。classApiKey有属性t.datetime:expires_at但是,如果删除了验证,则before_create方法会在创建时起作用。这是为什么?-此模式适用于其他属性,例如access_tokens(字符串)等 最佳答案 我会说因为before_create在验证之后运行,也许您想将before_create替换为before_validation注意:如果你像那样留下回调,它会在你运行valid?或save或任何时候设置到期日期触发验证的事件记录方法

ruby-on-rails - 将字符串转换为 datetime ruby​​ on rails

我知道这是基本的,但我已经苦苦挣扎了几个小时,我似乎无法应用将字符串转换为日期时间的多种方法中的一种,因此我可以将其保存在数据库中格式2018-03-1600:12:17.555372。提前致谢这是控制台输出的字符串。params[:event][:start_date]"03/28/20181:46AM"[编辑]根据一些线索,我想出了一些非常肮脏的东西,也许有人可以帮助重构我正在抑制AM或PM,因为我不知道如何解析我知道这太糟糕了,我们将不胜感激!if!params[:event][:start_date].empty?start_date=params[:event][:start

ruby-on-rails - 为什么介于两者之间? ruby 中 Date 和 DateTime 的工作方式不同?

doc说:between?(min,max)publicReturnstrueifthecurrentobject’stimeiswithinthespecifiedminandmaxtime.在ruby中:>>DateTime.now.between?(DateTime.now,DateTime.now+1)=>false>>Date.today.between?(Date.today,Date.today+1)=>true通过使用.current在Rails中,差异变得更加明显,因为您特别假设此方法在DateTime上会有类似的行为。和Date:>>DateTime.current

ruby - 在 Ruby 中验证 DateTime 对象

以下代码中的构造函数在参数中接收一个DateTime对象classTestattr_accessor:dateTime#TakesDateTimeobjectasinputdefinitialize(dateTime)@dateTime=dateTimeendend如何验证传递给构造函数的dateTime参数是否是有效的DateTime对象?找出答案的一种方法是使用:ifdateTime.class==DateTime但是有更好的方法吗?记住,dateTime是DateTime对象而不是字符串 最佳答案 你可以通过dateTime.

ruby-on-rails - 将字符串转换为 DateTime Ruby

我有一个字符串"2015-11-01T10:00:00.00+08:00"从json响应中提取。如何将其转换为时间或日期时间?我试过Time.new("2015-11-01T10:00:00.00+08:00")它返回2015-01-0100:00:00+0530,显然这里的日期和时间也发生了变化。 最佳答案 require'date'▶Date.parse"2015-11-01T10:00:00.00+08:00"#⇒#▶DateTime.parse"2015-11-01T10:00:00.00+08:00"#⇒#或者,甚至更好:

Ruby DateTime 人类时区输出

我正在尝试输出ruby​​DateTime对象的时区:DateTime.parse('2012/05/23').strftime('%Z')这会输出"+00:00"。根据文档,它应该返回GMT。我是做错了什么,还是发现了一个错误? 最佳答案 DateTime类似乎不支持区域数据作为区域名称。Time类却正确地做到了这一点。所以要么这样做:require'date'require'time'Time.parse('...').strftime('%Z')或者,如果您已经拥有DateTime格式的数据,那么:Time.parse(Dat

ruby-on-rails - 给定一个 DateTime,如何获取特定时区中具有相同日期的时间范围?

我有一个表示特定日期的DateTime对象,如“2011-01-1500:00:00UTC”表示1月15日。我想生成具有相同日期的特定时区的时间范围。方法签名可能是这样的defday_range_for(date,tz)#...end例如,如果我有range_for(DateTime.parse('2011-01-15'),'CST'),那么我希望结果是像2011这样的范围-01-1500:00:00-0600..2011-01-1523:59:59-0600. 最佳答案 你能用输入字符串代替对象吗?如果您传入一个DateTime对

ruby - 为什么 Ruby 的 DateTime.new_offset 不在 rdoc 中

我想创建一个DateTime使用来自thisSOFpost的答案的UTC时区实例使用方法DateTime#new_offset(0)。但是,我无法在DateTimerdoc或其父类的rdocDate中找到它的定义。.然而,DateTime确实定义了方法:DateTime.method_defined?:new_offset#=>truenew_offset没有出现在rdoc中一定有充分的理由。 最佳答案 为什么DateTime#new_offset没有出现在DateTime的RDoc中?因为它是在父类Date中定义的。为什么Date

ruby-on-rails - DateTime 类的意义何在?

DateTime类似乎是多余的,在阅读了该类的Rails扩展文档中的这一点之后,它似乎也有潜在的危险:DateTimesaren’tawareofDSTrules,souseaconsistentnon-DSToffsetwhencreatingaDateTimewithanoffsetinthelocalzoneDateTime#to_time的Rails文档中还有这个:AttemptstoconvertselftoaRubyTimeobject;returnsselfifoutofrangeofRubyTimeclass.Ifselfhasanoffsetotherthan0,se