【问题标题】:WebKit/Chrome Timestamp convert into Ruby/RailsWebKit/Chrome 时间戳转换成 Ruby/Rails
【发布时间】:2019-02-25 14:13:37
【问题描述】:

如何将 WebKit/Chrome 时间戳转换为 Ruby/Rails。

这是来自 Chrome excel 13130755192116927 的时间戳数据,但我如何使用 Ruby/Rails 将其转换为人类可读的格式。

我找到了一些像How to convert a unix timestamp (seconds since epoch) to Ruby DateTime? 这样的例子,但是这个数据长度是 13 而我的数据长度是 17。

我是如何做到这一点的WebKit/Chrome Timestamp Converter

GMT: Sunday, February 5, 2017 7:59:52 AM

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby unix-timestamp epoch


    【解决方案1】:

    来自this question

    Google 时间戳的格式为自以来的微秒数 1601 年 1 月

    这里是一个 Ruby 示例:

    require 'date'
    
    chrome_timestamp = 13130755192116927
    
    # Get the January 1601 unixepoch
    since_epoch = DateTime.new(1601,1,1).to_time.to_i
    
    # Transfrom Chrome timestamp to seconds and add 1601 epoch
    final_epoch = (chrome_timestamp / 1000000) + since_epoch
    
    # Print DateTime
    date = DateTime.strptime(final_epoch.to_s, '%s')
    
    # without formating
    puts date
    => '2017-02-05T07:59:52+00:00'
    
    # with formating
    puts date.strftime('%A, %B %-d, %Y %-I:%M:%S %p')
    => 'Sunday, February 5, 2017 7:59:52 AM'
    

    【讨论】:

    • 你能建议我如何得到这样的Sunday, February 5, 2017 7:59:52 AM
    • epochconverter.com/webkit 中显示7:59:52 AM 但在您的代码中显示07:50:31+00:00,请告知
    • @user9769384 试试这个DateTime.strptime(final_epoch.to_s, '%s').strftime("%A, %B %-d, %Y %-l:%M:%S %p")。基本上我只是添加了一个将其转换为所需格式的方法。
    • 已修复,我使用的是 Date.new 而不是 DateTime.new,现在它返回正确的日期并且我添加了格式化示例
    • 谢谢,我会尽快检查
    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 2016-08-23
    • 2019-09-11
    • 2014-01-23
    • 2015-03-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多