Skip to content

Commit

Permalink
feat: added new function _dateAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
fwalzel committed Aug 26, 2024
1 parent 98f86c7 commit 30a091c
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion dist/handlebars-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* handlebars-i18n.js
*
* @author: Florian Walzel
* @date: 2024-05
* @date: 2024-08
*
* handlebars-i18n adds features for localization/
* internationalization to handlebars.js
Expand Down Expand Up @@ -444,6 +444,65 @@
return dateFormat.format(date);
}
);
handlebars.registerHelper('_dateAdd',
/**
* adds a time offset in a given unit to a date
* -> returns the modified date
*
* @param dateInput
* @param offset
* @param unit
* @param options
* @returns {string}
*/
function (dateInput, offset, unit, options) {
const date = __createDateObj(dateInput);

unit = unit || 'hour';

switch (unit) {
case 'second':
case 'seconds':
date.setSeconds(date.getSeconds() + offset);
break;
case 'minute':
case 'minutes':
date.setMinutes(date.getMinutes() + offset);
break;
case 'hour':
case 'hours':
date.setHours(date.getHours() + offset);
break;
case 'day':
case 'days':
date.setDate(date.getDate() + offset);
break;
case 'week':
case 'weeks':
date.setDate(date.getDate() + offset * 7);
break;
case 'month':
case 'months':
date.setMonth(date.getMonth() + offset);
break;
case 'quarter':
case 'quarters':
date.setMonth(date.getMonth() + offset * 3);
break;
case 'year':
case 'years':
date.setFullYear(date.getFullYear() + offset);
break;
default:
throw new Error('@ handlebars-i18n: invalid argument "unit" was given for _dateAdd.' +
'Unit must be either "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year".');
}

const opts = __configLookup(options, i18next.language, optionsConf.DateTimeFormat);
const dateFormat = new Intl.DateTimeFormat(i18next.language, opts);
return dateFormat.format(date);
}
);
handlebars.registerHelper('_dateRel',
/**
* returns a relative date formatted according the options
Expand Down

0 comments on commit 30a091c

Please sign in to comment.