Bug 33804: Use as_due_date to display due dates
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / js-date-format.inc
1 [% USE Koha %]
2 [% USE raw %]
3 [% USE Asset %]
4 [% USE KohaDates %]
5 [% Asset.js("lib/dayjs/dayjs.min.js") | $raw %]
6 [% Asset.js("lib/dayjs/plugin/utc.js") | $raw %]
7 [% Asset.js("lib/dayjs/plugin/timezone.js") | $raw %]
8 [% Asset.js("lib/dayjs/plugin/customParseFormat.js") | $raw %]
9 <script>
10     dayjs.extend(window.dayjs_plugin_utc);
11     dayjs.extend(window.dayjs_plugin_timezone);
12     dayjs.extend(window.dayjs_plugin_customParseFormat);
13 </script>
14
15 <!-- js-date-format.inc -->
16 <script>
17     (function() {
18         var def_date_format = '[% Koha.Preference('dateformat') | html %]';
19         var def_time_format = '[% Koha.Preference('TimeFormat') | html %]';
20         var def_tz = '[% KohaDates.tz | html %]';
21
22         var get_date_pattern = function(format) {
23             var date_pattern = 'YYYY-MM-DD';
24             if(format == 'us') date_pattern = 'MM/DD/YYYY';
25             if(format == 'metric') date_pattern = 'DD/MM/YYYY';
26             if(format == 'dmydot') date_pattern = 'DD.MM.YYYY';
27             return date_pattern;
28         };
29
30         var get_time_pattern = function(format) {
31             var time_pattern = 'HH:mm';
32             if(format == '12hr') time_pattern = 'hh:mm a';
33             return time_pattern;
34         };
35
36         /*
37          * A JS equivilent of the KohaDates TT Plugin. Passed an rfc3339 formatted date string,
38          * or JS Date, the function will return a date string formatted as per the koha instance config.
39          * Optionally accepts a dateformat parameter to allow override of the configured output format
40          * as well as a 'withtime' boolean denoting whether to include time or not in the output string.
41          * 'as_due_date' can be passed to format a date time as a due date: The time part will not be displayed if 23:59.
42          */
43         window.$date = function(value, options) {
44             if(!value) return '';
45             let tz = (options&&options.tz)||def_tz;
46             let no_tz_adjust = (options&&options.no_tz_adjust)||false;
47             var m = dayjs(value);
48             if ( !no_tz_adjust && ! value.match(/^\d{4}-\d{2}-\d{2}$/ ) ) {
49                 m = m.tz(tz);
50             }
51
52             var dateformat = (options&&options.dateformat)||def_date_format;
53             var withtime = (options&&options.withtime)||false;
54
55             if(dateformat=='rfc3339' && withtime) return m.format();
56
57             var timeformat = (options&&options.timeformat)||def_time_format;
58             var date_pattern = get_date_pattern(dateformat);
59             let as_due_date = (options&&options.as_due_date);
60             if (as_due_date) {
61                 withtime = !( m.hour() == 23 && m.minute() == 59 );
62             }
63             var time_pattern = !withtime?'':' '+get_time_pattern(timeformat);
64
65             return m.format(date_pattern+time_pattern);
66         }
67
68         window.$datetime = function(value, options) {
69             options = options||{};
70             options.withtime = true;
71             return $date(value, options);
72         };
73
74         window.$time = function(value, options) {
75             if(!value) return '';
76             var tz = (options&&options.tz)||def_tz;
77             var m = dayjs(value);
78             if(tz) m = m.tz(tz);
79
80             var dateformat = (options&&options.dateformat);
81             var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format;
82
83             return m.format(get_time_pattern(timeformat)+(dateformat=='rfc3339'?':ss'+(!m.isUTC()?'Z':''):''))+(dateformat=='rfc3339' && m.isUTC()?'Z':'');
84         }
85
86         window.$date_to_rfc3339 = function(value, options) {
87             var dateformat = (options&&options.dateformat)||def_date_format;
88             let m = dayjs(value, get_date_pattern(dateformat));
89             return m.format("YYYY-MM-DD");
90         }
91
92     })();
93 </script>
94 <!-- / js-date-format.inc -->