From 54b2e4698c56d12f32e41c4b6e12f730b25effdc Mon Sep 17 00:00:00 2001 From: Bohung Date: Thu, 10 Feb 2022 17:47:38 +0800 Subject: [PATCH] Fix date format bug. --- app/models/event_news.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 9ded48a..49e12cd 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -174,11 +174,21 @@ class EventNews parse_format = self.date_parse_format if s.to_date == e.to_date parse_format_arr = parse_format.split(/(%d %a|%d) /) - parse_format_arr = parse_format_arr[0..-2].join(""), parse_format_arr[-1] + if parse_format_arr.count > 1 + parse_format_arr = parse_format_arr[0..-2].join(""), parse_format_arr[-1] + end date_str = custom_strftime(s, parse_format_arr[0]) - s_time = s.strftime(parse_format_arr[1]) - e_time = e.strftime(parse_format_arr[1]) - "#{date_str} #{s_time} ~ #{e_time}" + s_time = s.strftime(parse_format_arr[1].to_s) + e_time = e.strftime(parse_format_arr[1].to_s) + if e_time.present? + if s_time != e_time + "#{date_str} #{s_time} ~ #{e_time}" + else + "#{date_str} #{s_time}" + end + else + date_str + end else custom_strftime(s, self.date_parse_format) + " ~ " + custom_strftime(e, self.date_parse_format) end