Convert 12 hr time to 24 hr format in Ruby

前端 未结 4 467
后悔当初
后悔当初 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:37

    Your question is unclear, but it sounds like you have a string "10pm", and you need to (1) capture it as a Time, and (2) represent that Time in 24-hour format. I would do it like this. First, gem install chronic, then, write a script like this:

    require 'chronic'
    t = Chronic.parse('10pm')
    p t.strftime("%H:%M")
    

    Returns "22:00"

提交回复
热议问题