Rails undefined method` []'for 2011-03-16 18:58:00 UTC:Time

时间:2011-03-16 20:39:27

标签: ruby-on-rails datetime string

使用红宝石和铁轨的新手...也许我正在为自己做更多工作......但无法将时区更改为本地我决定解析日期和时间并将其用于我自己的目的。除了子串不适合我。

<% ds = thejob.datestamp 
       dsyy = ds[0,4]
       dsmm = ds[5,2]   
       dsdd = ds[8,2]   
       dshrs = ds[11,2]   
       dsmin = ds[14,2]   
       dssec = ds[17,2]   
       dstz = ds[20,2] %>

导致错误:

undefined method `[]' for 2011-03-16 18:58:00 UTC:Time

2 个答案:

答案 0 :(得分:2)

您确定datestamp实际上是字符串,而不是Time类型值吗?

根据Ruby date/time documentation判断,您可能只需要:

local = thejob.datestamp.getlocal

答案 1 :(得分:0)

ds = thejob.datestamp 
dsyy = ds.year
dsmm = ds.month   
dsdd = ds.day  
dshrs = ds.hour
dsmin = ds.min
dssec = ds.sec  
dstz = I don't remember