From e9308332256a194e6b0edad032598d84241decd7 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 31 May 2022 15:09:43 +0200
Subject: [PATCH] Bug 30310: Adjust js-date-format

Test plan:
Confirm that the test plan from bug 24455 is still passing

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 .../prog/en/includes/js-date-format.inc       | 20 +++++++++++-----
 .../bootstrap/en/includes/js-date-format.inc  | 24 +++++++++++++------
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc
index 6b302166c9..f8bff29d8f 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc
@@ -2,8 +2,14 @@
 [% USE raw %]
 [% USE Asset %]
 [% USE KohaDates %]
-[% Asset.js("lib/moment/moment.min.js") | $raw %]
-[% Asset.js("lib/moment/moment-timezone-with-data-10-year-range.min.js") | $raw %]
+[% Asset.js("lib/dayjs/dayjs.min.js") | $raw %]
+[% Asset.js("lib/dayjs/plugin/timezone.js") | $raw %]
+[% Asset.js("lib/dayjs/plugin/utc.js") | $raw %]
+<script>
+    dayjs.extend(window.dayjs_plugin_utc);
+    dayjs.extend(window.dayjs_plugin_timezone);
+</script>
+
 <!-- js-date-format.inc -->
 <script>
     (function() {
@@ -34,8 +40,10 @@
         window.$date = function(value, options) {
             if(!value) return '';
             var tz = (options&&options.tz)||def_tz;
-            var m = moment(value);
-            if((m.creationData().format !== 'YYYY-MM-DD')&&tz) m.tz(tz);
+            var m = dayjs(value);
+            if ( ! value.match(/^\d{4}-\d{2}-\d{2}$/ ) ) {
+                m = m.tz(tz);
+            }
 
             var dateformat = (options&&options.dateformat)||def_date_format;
             var withtime = (options&&options.withtime)||false;
@@ -58,8 +66,8 @@
         window.$time = function(value, options) {
             if(!value) return '';
             var tz = (options&&options.tz)||def_tz;
-            var m = moment(value);
-            if(tz) m.tz(tz);
+            var m = dayjs(value);
+            if(tz) m = m.tz(tz);
 
             var dateformat = (options&&options.dateformat);
             var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format;
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc
index 643cda065b..4f6b08db6d 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc
@@ -2,8 +2,15 @@
 [% USE raw %]
 [% USE Asset %]
 [% USE KohaDates %]
-[% Asset.js("lib/moment.min.js") | $raw %]
-[% Asset.js("lib/moment-timezone-with-data-10-year-range.min.js") | $raw %]
+[% Asset.js("lib/dayjs/dayjs.min.js") | $raw %]
+[% Asset.js("lib/dayjs/plugin/timezone.js") | $raw %]
+[% Asset.js("lib/dayjs/plugin/utc.js") | $raw %]
+<script>
+    dayjs.extend(window.dayjs_plugin_utc);
+    dayjs.extend(window.dayjs_plugin_timezone);
+</script>
+
+<!-- js-date-format.inc -->
 <script>
     (function() {
         var def_date_format = '[% Koha.Preference('dateformat') | html %]';
@@ -33,8 +40,10 @@
         window.$date = function(value, options) {
             if(!value) return '';
             var tz = (options&&options.tz)||def_tz;
-            var m = moment(value);
-            if((m.creationData().format !== 'YYYY-MM-DD')&&tz) m.tz(tz);
+            var m = dayjs(value);
+            if ( ! value.match(/^\d{4}-\d{2}-\d{2}$/ ) ) {
+                m = m.tz(tz);
+            }
 
             var dateformat = (options&&options.dateformat)||def_date_format;
             var withtime = (options&&options.withtime)||false;
@@ -56,9 +65,9 @@
 
         window.$time = function(value, options) {
             if(!value) return '';
-            var tz = (opitons&&options.tz)||def_tz;
-            var m = moment(value);
-            if(tz) m.tz(tz);
+            var tz = (options&&options.tz)||def_tz;
+            var m = dayjs(value);
+            if(tz) m = m.tz(tz);
 
             var dateformat = (options&&options.dateformat);
             var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format;
@@ -68,3 +77,4 @@
 
     })();
 </script>
+<!-- / js-date-format.inc -->
-- 
2.30.2