From 6feb7d7705a069921bdc2673f47735fffa78c503 Mon Sep 17 00:00:00 2001 From: FinnIckler Date: Fri, 10 Jan 2025 12:32:05 +0100 Subject: [PATCH] just use luxon Interval --- app/webpacker/lib/utils/dates.js | 63 +------------------------------- 1 file changed, 2 insertions(+), 61 deletions(-) diff --git a/app/webpacker/lib/utils/dates.js b/app/webpacker/lib/utils/dates.js index 358714bd3e..73fad885f7 100644 --- a/app/webpacker/lib/utils/dates.js +++ b/app/webpacker/lib/utils/dates.js @@ -1,4 +1,4 @@ -import { DateTime } from 'luxon'; +import { DateTime, Interval } from 'luxon'; import I18n from '../i18n'; // parameter name conventions: // - `luxonDate` for luxon DateTime objects @@ -129,65 +129,6 @@ function getMonthNames(format = 'short') { return Array.from({ length: 12 }, (_, i) => formatter.format(new Date(2000, i))); } -// Translated from https://github.com/mbillard/time_will_tell/blob/master/lib/time_will_tell/helpers/date_range_helper.rb export function dateRange(fromDate, toDate, options = {}) { - const format = options.format || 'short'; - const scope = options.scope || 'time_will_tell.date_range'; - const separator = options.separator || '—'; - const showYear = options.showYear !== false; - - const monthNames = getMonthNames(format); - - let fromDateTime = DateTime.fromISO(fromDate); - let toDateTime = DateTime.fromISO(toDate); - - if (fromDateTime > toDateTime) { - [fromDateTime, toDateTime] = [toDateTime, fromDateTime]; - } - - const fromDay = fromDateTime.day; - const fromMonth = monthNames[fromDateTime.month - 1]; - const fromYear = fromDateTime.year; - const toDay = toDateTime.day; - - const dates = { from_day: fromDay, sep: separator }; - let template; - - if (fromDateTime.hasSame(toDateTime, 'day')) { - template = 'same_date'; - Object.assign(dates, { month: fromMonth, year: fromYear }); - } else if (fromDateTime.hasSame(toDateTime, 'month') && fromDateTime.hasSame(toDateTime, 'year')) { - template = 'same_month'; - Object.assign(dates, { to_day: toDay, month: fromMonth, year: fromYear }); - } else { - const toMonth = monthNames[toDateTime.month - 1]; - - Object.assign(dates, { - from_month: fromMonth, - to_month: toMonth, - to_day: toDay, - }); - - if (fromDateTime.hasSame(toDateTime, 'year')) { - template = 'different_months_same_year'; - dates.year = fromYear; - } else { - const toYear = toDateTime.year; - template = 'different_years'; - Object.assign(dates, { from_year: fromYear, to_year: toYear }); - } - } - - const withoutYear = I18n.t(`${scope}.${template}`, dates); - - if (showYear && fromDateTime.hasSame(toDateTime, 'year')) { - return ( - I18n.t(`${scope}.with_year`, { - date_range: withoutYear, - year: fromYear, - defaultValue: withoutYear, - }) || withoutYear - ); - } - return withoutYear; + return Interval.fromDateTimes(DateTime.fromISO(fromDate), DateTime.fromISO(toDate)).toLocaleString({ month: 'short', day: '2-digit', year: 'numeric' }, options); }