【问题标题】:'Time.now' not showing the right time'Time.now' 没有显示正确的时间
【发布时间】:2018-02-05 08:57:40
【问题描述】:

我想使用Time.now.strftime("%d/%m/%Y %H:%M") 来显示我所在时区的当前时间,但似乎延迟了两个小时。它显示:28/08/2017 06:36,当它是08:36。如何调整时区以显示正确的时间?

【问题讨论】:

  • Time.now.getlocal.zone 这是返回您的区域吗?

标签: ruby time timezone


【解决方案1】:

纯 Ruby 方式:

> Time.now.utc.localtime("+05:30").strftime("%d/%m/%Y  %H:%M")
#=> "28/08/2017  12:41"

其中+05:30 (IST) 是特定区域的偏移量

注意:Time.now.getlocal.zone 将返回您的本地区域


使用 ActiveSupport

>  require 'active_support/time'
#=> true 
> Time.now.in_time_zone('US/Eastern').strftime("%d/%m/%Y  %H:%M")
#=> "28/08/2017  03:17"

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 2013-04-08
    • 2021-03-31
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多