将unix时间戳格式化为可读的内容

时间:2013-07-30 15:59:56

标签: ruby-on-rails cookies google-analytics unix-timestamp

我正试图通过谷歌分析cookie解析器解析一些比特,我有这个:

:Time_of_first_visit__c => Time.new(@data.utma_hash.fetch(:initial_visit_at)).to_datetime.to_formatted_s(:long),
:Time_of_previous_visit__c => Time.new(@data.utma_hash.fetch(:previous_visit_at)).to_datetime.to_formatted_s(:long),
:Current_visit_time__c => Time.new(@data.utma_hash.fetch(:current_visit_at)).to_datetime.to_formatted_s(:long),

但它的呈现方式如下:

首次访问时间 1月01日,1375174064 00:00

目前的访问时间 1月01日,1375174064 00:00

上次访问的时间 1月01日,1375174064 00:00

2 个答案:

答案 0 :(得分:0)

尝试删除to_formatted_s(:long)并将其用作I18n.l

的参数

示例:

I18n.l Time.new(@data.utma_hash.fetch(:initial_visit_at)).to_datetime

当然,您需要使用您的语言.yml文件指定日期时间格式,但它应该使用您可以在官方指南中找到的默认格式

答案 1 :(得分:0)

这是我最后使用的代码:

:Time_of_first_visit__c => Time.at(@data.utma_hash.fetch(:initial_visit_at).to_i + 1.hour).strftime("%B %e, %Y at %I:%M %p"),
:Time_of_previous_visit__c => Time.at(@data.utma_hash.fetch(:previous_visit_at).to_i + 1.hour).strftime("%B %e, %Y at %I:%M %p"),
:Current_visit_time__c => Time.at(@data.utma_hash.fetch(:current_visit_at).to_i + 1.hour).strftime("%B %e, %Y at %I:%M %p"),