草庐IT

charged_datetime

全部标签

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

ruby - 从 UTC 秒数创建 DateTime

我不敢相信我在任何地方都找不到这个,但是你怎么能从UTC秒创建一个DateTime?类似于在Java中,您可以通过Calendar.getInstance().setTimeInMillis(time)获取特定时间。DateTime.new1344899588没有工作,我在DateTimeAPI文档中没有看到任何说明如何执行此操作的内容。这行得通,但似乎是一种迂回的方式:DateTime.strptime(1344899588.to_s,"%s") 最佳答案 您可以为此使用Time类,即t=Time.at(1344899588)如果

ruby - 如何从 Ruby 中的 DateTime 中减去 n 个月?

我是ruby​​的新手,日期算术看起来很困惑。如何在不使用任何其他模块的情况下从DateTime中删除n个月? 最佳答案 自DateTime是Date的子类,你可以使用或prev_month:require'date'd=DateTime.now#=>#d#d.prev_month(4)#=>#请注意DateTime不考虑夏令时。 关于ruby-如何从Ruby中的DateTime中减去n个月?,我们在StackOverflow上找到一个类似的问题: https

ruby - Podio:设置 DateTime 字段值时使用哪个时区

使用跑道API创建新项目或更新现有项目时,将DateTime字段值设置为:2016-10-2114:15:00(例如)。将使用哪个时区来存储此DateTime?例如要求:app_id=content={'title'=>'Datesetto"14:15"','date'=>{'start'=>'2016-10-2114:15:00','end'=>'2016-10-2115:00:00'}}item=Podio::Item.create(app_id,'fields'=>content)结果:'start_date_utc'=>2016-10-21'end'=>2016-10-2115