Converting Time in UTC to Pacific time

后端 未结 3 1085
闹比i
闹比i 2021-02-07 11:17

I get a string from a external method with a time and date like so \"07/09/10 14:50\" is there any way I can convert that time in ruby to \'Pacific US\' time knowin

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 11:24

    Since it appears you are using rails, you have quite a few options. I suggest reading this article that talks all about time zones.

    To convert to PST, both of these are rails-specific methods. No need to re-invent the wheel:

    time = Time.parse("07/09/10 14:50")
    time.in_time_zone("Pacific Time (US & Canada)")
    

    Hope this helps

    UPDATE: rails might try to get smart and give the time you specify as a string a time zone. To ensure that the time parses as UTC, you should specify in the string:

    time = Time.parse("07/09/10 14:50 UTC")
    time.in_time_zone("Pacific Time (US & Canada)")
    

提交回复
热议问题