在Ruby中将12小时转换为24小时格式 [英] Convert 12 hr time to 24 hr format in Ruby

查看:82
本文介绍了在Ruby中将12小时转换为24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将11am和10pm转换为11:00和22:00?有没有一个使用日期和时间类的简单方法?

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

推荐答案

我先用 Time#strptime ,然后用 Time#strftime 输出。这样可以确保您的原始格式的严格检查。

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"

这篇关于在Ruby中将12小时转换为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆