Converting UTC timestamp to ISO 8601 in Ruby

前端 未结 4 2078
礼貌的吻别
礼貌的吻别 2020-12-24 04:36

I have a timestamp that is in UTC

\"2010-10-25 23:48:46 UTC\"

I need to convert it into ISO 8601

\"2010-10-29 06:09Z\"

The documentation is

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 05:04

    Adding an answer to this super old question because if you're using Rails, there is no need to convert/parse it in the way the other answers here are telling you to do:

    • If it's already a timestamp (e.g., a created_at or updated_at attribute), you can directly call the iso8601 method on that timestamp (e.g., object.created_at.iso8601).
    • The iso8601 method also accepts a numeric argument to display fractional digits; you would use this if you want 2020-04-06T19:16:55.604Z instead of 2020-04-06T19:16:55Z.

    https://api.rubyonrails.org/v5.2.4/classes/ActiveSupport/TimeWithZone.html#method-i-iso8601

提交回复
热议问题