红宝石中是否有一个函数可以用文字显示日期和日期?

时间:2015-11-03 03:44:32

标签: ruby ruby-on-rails-4

我有以下日期,

  1. from_date(11-15-2013)和to_date(11-30-2013)
  2. from_date(11-30-2013)和to_date(12-15-2013)
  3. 现在我想用文字显示它来说

    "And then choose the opponent you would like to attack from [a,b]"
    

    是否存在执行此操作的ruby日期类方法?

2 个答案:

答案 0 :(得分:1)

假设你有:

date_str = "11-15-2013"

你知道日期格式是" mm-dd-yyyy"。然后第一步是使用类方法Date::strptimedate_str转换为对象的日期:

require 'date'

date_obj = Date.strptime(date_str, '%m-%d-%Y')
  #=> #<Date: 2013-11-15 ((2456612j,0s,0n),+0s,2299161j)> 

我们现在使用Date类中的各种方法来提取感兴趣的信息:

month = date_obj.month #=> 11
day   = date_obj.day   #=> 15
year  = date_obj.year  #=> 2013
wday  = date_obj.wday  #=> 5

日期类还提供了一些有用的常量,包括:

Date::MONTHNAMES
  #=> [nil, "January", "February", "March", "April", "May", "June",
  #    "July", "August", "September", "October", "November", "December"]

Date::ABBR_MONTHNAMES
  #=> [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
  #    "Sep", "Oct", "Nov", "Dec"] 

Date::DAYNAMES
 #=> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

请注意,Date::MONTHNAMESDate::ABBT_MONTHNAMESnil)的第一个元素从未被引用,因为1月是月1,而不是月0

我们现在可以构造如下字符串:

"Today is day #{day} of #{Date::MONTHNAMES[month]}, a #{Date::DAYNAMES[wday]}."
  #=> "Today is day 15 of November, a Friday." 

"Oh, I forgot, the year is #{year}." 
  #=> "Oh, I forgot, the year is 2013.

所以做你想做的是这些方法的相当简单的应用:

require 'date'

def spell_out_date_ranges(*date_ranges)
  date_ranges.map { |start_str, end_str|
    spell_out_one_range(start_str, end_str) }.join(' ')
end 

def spell_out_one_range(start_str, end_str)
   sd = date_str_to_hash(start_str)
   ed = date_str_to_hash(end_str)
   if sd[:month] == ed[:month] && sd[:year] == ed[:year]
     "%s. %d - %d, %d" % [sd[:month_name], sd[:day], ed[:day], sd[:year]]
   elsif sd[:year] == ed[:year]
     "%s. %d - %s. %d, %d" % [sd[:month_name], sd[:day], ed[:month_name],
       ed[:day], sd[:year]]
   else
     "%s. %d, %d - %s. %d, %d" % [sd[:month_name], sd[:day], sd[:year],
       sd[:month_name], ed[:day], ed[:year]]
   end
 end

def date_str_to_hash(date_str)
  d = Date.strptime(date_str, '%m-%d-%Y')
  { day: d.day, month: d.month, month_name: Date::ABBR_MONTHNAMES[d.month],
    year: d.year }
end

让我们试一试:

spell_out_date_ranges(['11-15-2013', '11-30-2013'], ['11-30-2013', '12-15-2013'],
                      ['11-30-2013', '12-15-2014'])
  #=> "Nov. 15 - 30, 2013 Nov. 30 - Dec. 15, 2013 Nov. 30, 2013 - Nov. 15, 2014" 

我在这里使用方法String#%来格式化字符串。格式语法在方法Kernel#sprintf的文档中进行了解释。

答案 1 :(得分:0)

没有ruby日期类方法可以直接制作,你应该自己做点什么。您可以使用strftime方法格式化时间。

Date.new(2015,12,30).strftime("%b") # get The abbreviated month name 
=> "Dec"
Date.new(2015,12,30).strftime("%Y-%m-%d") # get the year, month, day
=> "2015-12-30"
Date.parse("30-12-2015") # convert string to date
=> Wed, 30 Dec 2015
你可以这样做:

def func(date1, date2)
  date1, date2 = date2, date1 if date1 > date2
  if date1.year == date2.year and date1.month == date2.month
    return [date1.strftime("%b.%d"), date2.strftime("%d,%Y")].join('-')
  elsif date1.year == date2.year and date1.month != date2.month
    return [date1.strftime("%b.%d"), date2.strftime("%b.%d,%Y")].join('-')
  elsif date1.year != date2.year
    return [date1.strftime("%b.%d,%Y"), date2.strftime("%b.%d,%Y")].join('-')
  end
end

func(Date.new(2015,11,15), Date.new(2015,11,30))
=> "Nov.15-30,2015"
func(Date.new(2015,11,15), Date.new(2015,12,30))
=> "Nov.15-Dec.30,2015"
func(Date.new(2014,11,15), Date.new(2015,12,30))
=> "Nov.15,2014-Dec.30,2015"