【问题标题】:Format unix timestamp to something readable将 unix 时间戳格式化为可读的内容
【发布时间】:2013-07-31 16:54:34
【问题描述】:

我正在尝试通过谷歌分析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),

但它是这样渲染的:

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

当前访问时间 1375174064 1 月 1 日 00:00

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

【问题讨论】:

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


    【解决方案1】:

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

    示例:

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

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

    【讨论】:

      【解决方案2】:

      这是我最后使用的代码:

      :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"),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 2020-12-12
        • 2015-09-08
        • 2014-09-21
        • 2020-10-23
        • 2011-05-29
        相关资源
        最近更新 更多