Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将日期转换为字符串,比如输入11-12-2001输出应该是2001年12月11日,在ruby中_Ruby - Fatal编程技术网

如何将日期转换为字符串,比如输入11-12-2001输出应该是2001年12月11日,在ruby中

如何将日期转换为字符串,比如输入11-12-2001输出应该是2001年12月11日,在ruby中,ruby,Ruby,如何在ruby中将中的日期转换为字符串 输入日期 2-12-2002 输出 2002年12月2日这是一个部分选项,使用: 如果您还想拼写数字,请查看以下内容: 是否愿意分享任何尝试?2002->“2000”似乎是个错误。为什么要使用日期字符串.gsub('-','/')?只需将以下行更改为Date.strtime(日期字符串,“%d-%m-%Y”)。是的!我为什么这么做?谢谢您还可以执行month\u words[month.to\u i-1]以避免创建哈希。@tadman。谢谢你的建议。

如何在ruby中将中的日期转换为字符串 输入日期 2-12-2002 输出
2002年12月2日

这是一个部分选项,使用:

如果您还想拼写数字,请查看以下内容:


是否愿意分享任何尝试?2002->“2000”似乎是个错误。为什么要使用
日期字符串.gsub('-','/')
?只需将以下行更改为
Date.strtime(日期字符串,“%d-%m-%Y”)
。是的!我为什么这么做?谢谢您还可以执行
month\u words[month.to\u i-1]
以避免创建哈希。@tadman。谢谢你的建议。那将是一个更好的方法。我不能使用图书馆。我必须只使用ruby本机方法来实现这一点。没有指定输入,我的输入应该由用户在任何日期进行
require 'date'

date_string = "2-12-2002"
date = Date.strptime(date_string, "%d-%m-%Y")

p date.day # => 2
p date.month # => 12
p date.year # => 2002

p date.strftime("%d %B %Y") # => "02 December 2002"
require 'humanize'
day, month, year = '2-12-2002'.split('-')
month_numeric_range = (1..12).to_a.map(&:to_s)
month_words = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month_numeric_to_words = Hash[month_numeric_range.zip(month_words)]
day.to_i.humanize.capitalize + ' ' +  months_numeric_to_words[month] + ' ' + year.to_i.humanize.capitalize
# => "Two December Two thousand and two"