Ruby - 将字符串转换为日期 [英] Ruby - convert string to date

查看:123
本文介绍了Ruby - 将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如2011-06-02T23:59:59 + 05:30。



我想将其转换为日期格式,只需解析日期2011-06-02。

解决方案

对于Ruby 1.9.2:

  require'date'#If还没有要求如果在Rails中,那么你不需要这一行)。 
放入DateTime.parse(2011-06-02T23:59:59 + 05:30)to_date.to_s


I have a string like "2011-06-02T23:59:59+05:30".

I want to convert it to date format and need to parse only the date, "2011-06-02".

解决方案

For Ruby 1.9.2:

require 'date'    # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s

这篇关于Ruby - 将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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