Skip to content

Commit

Permalink
Merge pull request #77 from Aller-Couleur/feature/240814_func_DateAdd
Browse files Browse the repository at this point in the history
Feature/240814 func date add
  • Loading branch information
fwalzel authored Sep 3, 2024
2 parents 98f86c7 + 5a0e2f8 commit 2469e09
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 223 deletions.
67 changes: 66 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,71 @@
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 options
* @returns {string}
*/
function (dateInput, offset, options) {

if (typeof dateInput !== 'number' && typeof dateInput !== 'string')
throw new Error('@ handlebars-i18n: invalid first argument "dateInput" was given for _dateAdd.');

if (typeof offset !== 'number')
throw new Error('@ handlebars-i18n: invalid second argument "offset" was given for _dateAdd.');

const date = __createDateObj(dateInput);

const opts = __configLookup(options, i18next.language, optionsConf.DateTimeFormat);
opts.unit = opts.unit || 'hour';

switch (opts.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 dateFormat = new Intl.DateTimeFormat(i18next.language, opts);
return dateFormat.format(date);
}
);
handlebars.registerHelper('_dateRel',
/**
* returns a relative date formatted according the options
Expand Down
2 changes: 1 addition & 1 deletion dist/handlebars-i18n.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions examples/browser-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ <h3>Relative date representation</h3>
<button onclick="changeLang()">{{__ "key0"}} {{#if (localeIs "en")}}German {{else}}Englisch {{/if}}</button>
<h4>Date with positive time offset:</h4>
<code>{{{{raw}}}} {{_dateAdd "December 17, 1995" 24 unit="hour"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateAdd "December 17, 1995 08:00:00" 24 unit="hour"}}</p>
<h4>Date with negative time offset:</h4>
<code>{{{{raw}}}} {{_dateAdd "December 17, 1995" -10 unit="day"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateAdd "December 17, 1995" -10 unit="day"}}</p>
<h4>Relative difference between two dates:</h4>
<code>{{{{raw}}}} {{_dateDiff "1996-12-07T00:00:00" "1996-12-08T00:00:00" unit="day"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateDiff "1996-12-07T00:00:00" "1996-12-08T00:00:00" unit="day"}}</p>
<h4>Relative difference between two dates with custom configuration (subset "date-rel-spec"):</h4>
<code>{{{{raw}}}} {{_dateDiff myDate "1995-12-17T00:00:00" format="date-rel-spec"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateDiff myDate "1995-12-17T00:00:00" format="date-rel-spec"}}</p>
<h4>Relative date event in the future:</h4>
<code>{{{{raw}}}} {{_dateRel 7 unit="hour"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateRel 7 unit="hour"}}</p>
Expand All @@ -226,15 +242,7 @@ <h4>Relative date event in the past:</h4>
<code>{{{{raw}}}} {{_dateRel 7 unit="hour"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateRel -7 unit="hour"}}</p>
<h4>Date difference:</h4>
<code>{{{{raw}}}} {{_dateDiff "1996-12-07T00:00:00" "1996-12-08T00:00:00" unit="day"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateDiff "1996-12-07T00:00:00" "1996-12-08T00:00:00" unit="day"}}</p>
<h4>Date difference by custom configuration (subset "date-rel-spec"):</h4>
<code>{{{{raw}}}} {{_dateDiff mydate "1995-12-17T00:00:00" format="date-rel-spec"}} {{{{/raw}}}}</code>
<p>&#x2192; {{_dateDiff mydate "1995-12-17T00:00:00" format="date-rel-spec"}}</p>
<p>&nbsp;</p><button onclick="changeLang()">{{__ "key0"}} {{#if (localeIs "en")}}German {{else}}Englisch {{/if}}</button>`;
<button onclick="changeLang()">{{__ "key0"}} {{#if (localeIs "en")}}German {{else}}Englisch {{/if}}</button>`;

// -- Ignore this. It’s just a helper to display un-rendered {{handlebars code}} between the <code> Tags
Handlebars.registerHelper('raw', function(options) {
Expand Down
Loading

0 comments on commit 2469e09

Please sign in to comment.