我正在嘗試將此日期決議Wed, 17 Feb 2021 13:00:00 0100為 this 2021-02-17 13:00:00.000000000 0100。
我試過用這個Time.strptime(current_time.to_s, '%Q'),(current_time上面的日期)但我明白了1970-01-01 01:00:02.021 0100
但我不明白為什么我還要約會,你能幫幫我嗎?謝謝!
uj5u.com熱心網友回復:
我正在嘗試決議這個日期
Wed, 17 Feb 2021 13:00:00 0100[...]
您似乎已經有一個實體Time:(或者ActiveSupport::TimeWithZone它是 Rails 的替代品,具有更好的時區支持)
current_time = Time.current
#=> Thu, 19 May 2022 10:09:58.702560000 CEST 02:00
在這種情況下,沒有什么可以決議的。你只需要通過你喜歡的方式格式化它:strftime
current_time.strftime('%F %T.%N %z')
#=> "2022-05-19 10:09:58.702560000 0200"
僅當您想要將字串表示轉換為物件時才需要決議Time,例如:(使用 Rails 的Time.zone.parse變體)
time_string = 'Thu, 19 May 2022 10:09:58.702560000 CEST 02:00'
time_obj = Time.zone.parse(time_string)
#=> Thu, 19 May 2022 10:09:58.702560000 CEST 02:00
time_obj.class
#=> ActiveSupport::TimeWithZone
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/478518.html
