Bug 10950: Add intranet/OPAC form fields
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / js-patron-get-age.inc
1 <!-- js-patron-get-age.inc -->
2 [% USE KohaDates %]
3 <script>
4     (function() {
5
6         const tz = '[% KohaDates.tz | html %]';
7         window.$get_age = function(dob, options) {
8             if(!dob) return '';
9
10             let today = new Date();
11             dob = new Date(dob);
12             if ( tz ) {
13                 let today_tz = new Date(today.toLocaleString('en-US', {timeZone: tz}));
14                 let diff = today.getTime() - today_tz.getTime();
15                 today = new Date(today.getTime() - diff);
16
17                 let dob_tz = new Date(dob.toLocaleString('en-US', {timeZone: tz}));
18                 diff = dob.getTime() - dob_tz.getTime();
19                 dob = new Date(dob.getTime() - diff);
20             }
21             let age = today.getFullYear() - dob.getFullYear();
22             let m = today.getMonth() - dob.getMonth();
23             if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
24                 age--;
25             }
26             return age;
27         }
28
29     })();
30 </script>
31 <!-- / js-patron-get-age.inc -->