慢性有任何选择日期格式解析吗? (红宝石) [英] does chronic have any options of date format it parses? (ruby)
问题描述
需要告诉我们,日期格式是日 - 月是可能吗?我传给慢性的数据也可能是今天/昨天/ 2天前的话。
I need to tell chronic that the format of date is day-month-year is that possible? The data I pass to chronic could also be words today/yesterday/2 days ago.
目前慢性给我 2010年12月2日
而不是 2010年2月12日
从 12-02-2010
Currently chronic gives me 2 Dec 2010
instead of 12 Feb 2010
from 12-02-2010
我可以想到的唯一的解决方案是在将字符串传递给慢性之前,交换日期和月份。
require 'chronic'
puts "12-02-2010 = #{Chronic.parse('12-02-2010')}" #should be 12 Feb 2010
puts "yesteday = #{Chronic.parse('yesterday')}" #working ok
puts "Today = #{Chronic.parse('today')}" #working ok
推荐答案
今天我发现了这个问题几个月后被问到。似乎有一种方法可以指示交换月和日。只需使用:endian_precedence
选项:
I've found this question today, 20 months after it has been asked. It seems that there is a way to indicate to swap months and days. Just use the :endian_precedence
option:
:endian_precedence(Array) - 默认值: [:middle,:little] - 默认情况下,
慢性将解析03/04/2011作为第三个月的第四天。
或者,您可以通过将:endian_precedence
更改为[...]来告诉Chronic解析为第四个月
的第三天[ :little,
:middle]
示例:
Chronic.parse('12-02-2010').strftime('%d %b %Y') #=> 02 Dec 2010
Chronic.parse('12-02-2010', :endian_precedence => [:little, :median]).strftime('%d %b %Y') #=> 12 Feb 2010
希望这有帮助
Dorian
这篇关于慢性有任何选择日期格式解析吗? (红宝石)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!