Convert 12 hr time to 24 hr format in Ruby

前端 未结 4 477
后悔当初
后悔当初 2021-02-05 21:58

How do I convert \"11am\" and \"10pm\" into \"11:00\" and \"22:00\"? Is there a simple way using the date and time classes?

4条回答
  •  长发绾君心
    2021-02-05 22:15

    I would first parse the string with Time#strptime and then output it with Time#strftime. This ensures a strict check with your original format as well.

    require 'time'
    Time.strptime("10pm", "%I%P").strftime("%H:%M")
    => "22:00"
    

提交回复
热议问题