From f4444b15c9ea4f6fe1ec89a5294ba8802b5353d8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 6 May 2022 15:05:07 +0200 Subject: [PATCH] Bug 30673: Use moment.js to improve our date parsing --- .../prog/en/includes/calendar.inc | 1 + koha-tmpl/intranet-tmpl/prog/js/calendar.js | 37 +++---------------- koha-tmpl/intranet-tmpl/prog/js/members.js | 8 ++-- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc index 549d046cd7a..f69218fcc73 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc @@ -27,6 +27,7 @@ var flatpickr_timeformat = [% IF Koha.Preference('TimeFormat') == '12hr' %]false[% ELSE %]true[% END %]; +[% Asset.js("lib/moment/moment.min.js") | $raw %] [% Asset.js("js/calendar.js") | $raw %] [% Asset.js("lib/flatpickr/flatpickr.min.js") | $raw %] [% Asset.js("lib/flatpickr/shortcut-buttons-flatpickr.min.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/calendar.js b/koha-tmpl/intranet-tmpl/prog/js/calendar.js index 81561cc42d2..99da033ce0c 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/calendar.js +++ b/koha-tmpl/intranet-tmpl/prog/js/calendar.js @@ -5,29 +5,8 @@ if (debug > 1) { alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")"); } -function is_valid_date(date) { - // An empty string is considered as a valid date for convenient reasons. - if (date === '') return 1; - var dateformat = flatpickr_dateformat_string; - if (dateformat == 'us') { - if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; - dateformat = 'm/d/Y'; - } else if (dateformat == 'metric') { - if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; - dateformat = 'd/m/Y'; - } else if (dateformat == 'iso') { - if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0; - dateformat = 'Y-m-d'; - } else if (dateformat == 'dmydot') { - if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0; - dateformat = 'd.m.Y'; - } - try { - flatpickr.parseDate(date, dateformat); - } catch (e) { - return 0; - } - return 1; +function is_valid_date(dateText) { + return moment(dateText, get_dateformat_str(dateformat_pref).toUpperCase(), true ).isValid(); } function get_dateformat_str(dateformat) { @@ -58,20 +37,14 @@ function Date_from_syspref(dstring) { sentmsg++; alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n")); } - if (dateformat_pref === "iso") { - return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d) - } else if (dateformat_pref === "us") { - return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d) - } else if (dateformat_pref === "metric") { - return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d) - } else if (dateformat_pref === "dmydot") { - return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d) - } else { + let m = moment(dstring, get_dateformat_str(dateformat_pref).toUpperCase(), true); + if ( !m.isValid() ) { if (debug > 0) { alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref); } return 0; } + return m.toDate(); } function DateTime_from_syspref(date_time) { diff --git a/koha-tmpl/intranet-tmpl/prog/js/members.js b/koha-tmpl/intranet-tmpl/prog/js/members.js index 7a9f844a865..2d496184e8f 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/members.js +++ b/koha-tmpl/intranet-tmpl/prog/js/members.js @@ -129,8 +129,9 @@ function select_user(borrowernumber, borrower, relationship) { function CalculateAge(dateofbirth) { var today = new Date(); var dob = Date_from_syspref(dateofbirth); - var age = {}; + if (!dob) return; + var age = {}; age.year = today.getFullYear() - dob.getFullYear(); age.month = today.getMonth() - dob.getMonth(); var day = today.getDate() - dob.getDate(); @@ -152,10 +153,7 @@ function write_age() { hint.html(dateformat); var age = CalculateAge(document.form.dateofbirth.value); - - if (!age.year && !age.month) { - return; - } + if (!age) return; var age_string; if (age.year || age.month) { -- 2.25.1