【问题标题】:Ruby TimeZone Milliseconds Since Epoch At MidnightRuby TimeZone 自午夜以来的毫秒数
【发布时间】:2018-03-11 13:42:04
【问题描述】:

我需要获取自 1970 年以来的今天以毫秒为单位的时间,其中 BST(英国夏令时)偏移

a = DateTime.now
=> Fri, 29 Sep 2017 16:30:29 +0100

b = DateTime.new(a.year, a.month, a.day, 0, 0, 0, 0)
=> Fri, 29 Sep 2017 00:00:00 +0000   # this is an hour out  

b.strftime('%Q').to_i
1506643200000                        # therefore this is an hour out

我该如何纠正这个问题?

b = b.in_time_zone
=> Fri, 29 Sep 2017 01:00:00 BST +01:00  # looks promising but...

b.strftime('%Q').to_i
=> 0                                     # grrr

欢迎任何帮助

【问题讨论】:

    标签: ruby datetime timezone


    【解决方案1】:

    我找到了这个解决方案:

    a = DateTime.now
    => Fri, 29 Sep 2017 16:30:29 +0100
    
    a = a.beginning_of_day
    => Fri, 29 Sep 2017 00:00:00 +0100
    
    milliseconds_since_1970 = a.to_i * 1000
    => 1506639600000                     <-- correct!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      相关资源
      最近更新 更多