0

美しい日時間隔を書きたい。

のようなものを書くだけなら

"from #{date_start.strftime('%d.%m.%Y %H:%M')} 
 till #{date_end.strftime('%d.%m.%Y %H:%M')}"`

しかし、状況によっては、これは見栄えが悪くなります。例:

14.08.2012 00:00 から 16.08.2012 00:00 まで

それを人間化するにはどうすればよいですか?

4

1 に答える 1

2

より美しく表示するために、application_helper.rb にヘルパーを記述しました。

def humanize_date_range date_start, date_end

  date_to_show = [:Y, :m, :d]
  time_to_show = [:H, :M]

  date_to_show.delete(:Y) if date_start.year == date_end.year
  date_to_show.delete(:m) if date_start.month == date_end.month
  date_to_show.delete(:d) if date_start.day == date_end.day
  time_to_show.delete(:H) if date_start.hour == date_end.hour
  time_to_show.delete(:M) if date_start.min == date_end.min
  if time_to_show.empty? and date_to_show.empty?
    return "#{date_start.strftime('%d.%m.%Y')} in #{date_start.strftime('%H:%M')}"
  elsif time_to_show.size == 2 and date_to_show.empty?
    return "From #{date_start.strftime('%H:%M')} till #{date_end.strftime('%H:%M')} #{date_end.strftime('%d.%m.%Y')}"
  elsif date_to_show.size == 1 and time_to_show.empty? and date_to_show.index(:d)
    return "From #{date_start.strftime('%d')} till #{date_end.strftime('%d')} #{date_start.strftime('%b %Y')}"
  elsif time_to_show.empty? 
    return "From #{date_start.strftime('%d.%m.%Y')} till #{date_end.strftime('%d.%m.%Y')}"
  else
    return "C #{date_start.strftime('%d.%m.%Y %H:%M')} по #{date_end.strftime('%d.%m.%Y %H:%M')}"
  end
end

今はもっと美しく見えます:

2012年8月14日~16日

于 2012-08-14T11:22:02.353 に答える