Convert 12 hr time to 24 hr format in Ruby

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

    Are you saying you want 11am and 10pm and then the ability to 11:00 and 22:00 or do you want how to show 11:00 and 22:00 instead of 11am and 10pm?

    if its the second question here:

    %H - Hour of the day, 24-hour clock (00 to 23).

    #!/usr/bin/ruby -w
    time = Time.new
    
    puts time.to_s
    puts time.ctime
    puts time.localtime
    puts time.strftime("%H:%M")
    

    if its the first question tell:

    %I Hour of the day, 12-hour clock (01 to 12).

    #!/usr/bin/ruby -w
    time = Time.new
    timeSwitch = 1
    
    if (timeSwitch == 1)
        puts time.strftime("%H:%M")
    else 
        puts time.strftime("%I:%M")
    end
    

    Sorry about that had perl and ruby mixed up on syntax

提交回复
热议问题