[英]using regex in ruby to change format of date
I'm currently scraping the dates of various events from a website. 我目前正在从网站上抓取各种活动的日期。 The date is returned as
2015-04-27T20:00+00:00
". 该日期返回为
2015-04-27T20:00+00:00
“。
I can use a regular expression to get 2015-04-27
to appear, but am having trouble finding a way to format this date to 27-04-2015, eg dd-mm-yyyy
. 我可以使用正则表达式使
2015-04-27
出现,但是在寻找一种将日期格式设置为2015年4月27日的方法时遇到了麻烦,例如dd-mm-yyyy
。
Currently I have [/^[^\\T]*/]
. 目前我有
[/^[^\\T]*/]
。
I have searched other posts but to no avail, any help would be much appreciated. 我搜索了其他帖子,但无济于事,我们将不胜感激。
You can do it using Date
您可以使用
Date
来做
require 'date'
date = '2015-04-27T20:00+00:00'
puts Date.parse(date).strftime("%d-%m-%Y")
If you did want to do it with regex though you could use this ... 如果您确实想用正则表达式来做,尽管可以使用它...
irb(main):019:0> date = '2015-04-27T20:00+00:00'
=> "2015-04-27T20:00+00:00"
irb(main):020:0> date = date.sub(/^(\d+)-(\d+)-(\d+)T.*$/,'\3-\2-\1');
irb(main):021:0* date
=> "27-04-2015"
irb(main):022:0>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.