用红宝石分析字符串日期,例如“28-May-10” [英] Parsing string dates in ruby such as "28-May-10"

查看:108
本文介绍了用红宝石分析字符串日期,例如“28-May-10”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

Date.parse("28-May-10").to_s

返回 0010-5-28 (这是2000年关闭! )

Returns 0010-5-28 (which is 2000 years off!)

我如何获得红宝石来正确解释两位数年份。

How can I get ruby to interpret the two digit year properly.

有很多字符串日期转换技巧在谷歌,但大多数处理数字月份而不是五月。

There are plenty of string to date conversion tricks out there on google but most handle digit months as opposed to "May".

推荐答案

我更喜欢 Date.strptime 为此任务:

require 'date'
puts Date.strptime("28-May-10", "%d-%b-%y") #2010-05-28

这篇关于用红宝石分析字符串日期,例如“28-May-10”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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