创建以拼音表示毫秒的Time对象

jobtbby3  于 2022-09-21  发布在  Ruby
关注(0)|答案(1)|浏览(153)

我想要一个可以转换为is8601格式的时间对象:“2022-10-01T22:38:45.100Z”

我试过这个:

Time.utc(2022, 10, 1, 22, 38, 45, 100).in_time_zone.iso8601(3)

但这给了我“2022-10-01T22:38:45.000Z”,而不是“2022-10-01T22:38:45.100Z”毫秒信息不知何故消失了。它永远是000

我做错了什么?谢谢

5f0d552i

5f0d552i1#

你的比分是三个零:

>> Time.utc(2022, 10, 1, 22, 38, 45, 100)
=> 2022-10-01 22:38:45.0001 UTC

# ^

# NOTE: These are microseconds.

# aka `usec`

>> Time.utc(2022, 10, 1, 22, 38, 45, 100).usec
=> 100

乘以1000才能进入毫秒范围:

>> Time.utc(2022, 10, 1, 22, 38, 45, 123_000).in_time_zone.iso8601(3)
=> "2022-10-01T22:38:45.123Z"
  • https://rubyapi.org/3.1/o/time#method-c-utc*

相关问题