Koha/koha-tmpl/intranet-tmpl/prog/en/includes/js-patron-get-age.inc
Jonathan Druart 57a55528bc Bug 30059: Deal with timezones
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-03-24 14:22:10 -10:00

31 lines
1 KiB
C++

<!-- js-patron-get-age.inc -->
[% USE KohaDates %]
<script>
(function() {
const tz = '[% KohaDates.tz | html %]';
window.$get_age = function(dob, options) {
if(!dob) return '';
let today = new Date();
dob = new Date(dob);
if ( tz ) {
let today_tz = new Date(today.toLocaleString('en-US', {timeZone: tz}));
let diff = today.getTime() - today_tz.getTime();
today = new Date(today.getTime() - diff);
let dob_tz = new Date(dob.toLocaleString('en-US', {timeZone: tz}));
diff = dob.getTime() - dob_tz.getTime();
dob = new Date(dob.getTime() - diff);
}
let age = today.getFullYear() - dob.getFullYear();
let m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
age--;
}
return age;
}
})();
</script>
<!-- / js-patron-get-age.inc -->