
7

发现 Ruby 格式化日期有点奇怪, 1/Jan/2022:00:00:00 会格式化成 2021 年 1 月 1 日
source link: https://www.v2ex.com/t/825468
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

发现 Ruby 格式化日期有点奇怪, 1/Jan/2022:00:00:00 会格式化成 2021 年 1 月 1 日
justest123 · 11 小时 24 分钟前 · 556 次点击irb(main):019:0> require "date"
=> true
irb(main):020:0> Date.new(2022,1,1).to_s
=> "2022-01-01"
irb(main):021:0> Date.parse("2022-01-01").to_s
=> "2022-01-01"
irb(main):022:0> Date.parse("1st Jan 2022").to_s
=> "2022-01-01"
irb(main):023:0> Date.parse("1/Jan/2022:00:00:00 +0800").to_s
=> "2021-01-01"
irb(main):024:0> Date.parse("1/Jan/2022").to_s
=> "2022-01-01"
irb(main):025:0> Date.parse("1/Jan/2022:00:00:00").to_s
=> "2021-01-01"
偶然发现的现象,Nginx 日志里的这种格式的时间,带上时间或时间+时区后反而格式化错了,而 Date 支持的其他格式都没有问题,简单搜索一下没搜到是什么原因
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK