Bug 29234: Further clean Z3950 Tests
[srvgit] / koha-tmpl / opac-tmpl / bootstrap / 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/timezone.js") | $raw %]
7 [% Asset.js("lib/dayjs/plugin/utc.js") | $raw %]
8 <script>
9     dayjs.extend(window.dayjs_plugin_utc);
10     dayjs.extend(window.dayjs_plugin_timezone);
11 </script>
12
13 <!-- js-date-format.inc -->
14 <script>
15     (function() {
16         var def_date_format = '[% Koha.Preference('dateformat') | html %]';
17         var def_time_format = '[% Koha.Preference('TimeFormat') | html %]';
18         var def_tz = '[% KohaDates.tz | html %]';
19
20         var get_date_pattern = function(format) {
21             var date_pattern = 'YYYY-MM-DD';
22             if(format == 'us') date_pattern = 'MM/DD/YYYY';
23             if(format == 'metric') date_pattern = 'DD/MM/YYYY';
24             if(format == 'dmydot') date_pattern = 'DD.MM.YYYY';
25             return date_pattern;
26         };
27
28         var get_time_pattern = function(format) {
29             var time_pattern = 'HH:mm';
30             if(format == '12hr') time_pattern = 'hh:mm a';
31             return time_pattern;
32         };
33
34         /*
35          * A JS equivilent of the KohaDates TT Plugin. Passed an rfc3339 formatted date string,
36          * or JS Date, the function will return a date string formatted as per the koha instance config.
37          * Optionally accepts a dateformat parameter to allow override of the configured output format
38          * as well as a 'withtime' boolean denoting whether to include time or not in the output string.
39          */
40         window.$date = function(value, options) {
41             if(!value) return '';
42             var tz = (options&&options.tz)||def_tz;
43             var m = dayjs(value);
44             if ( ! value.match(/^\d{4}-\d{2}-\d{2}$/ ) ) {
45                 m = m.tz(tz);
46             }
47
48             var dateformat = (options&&options.dateformat)||def_date_format;
49             var withtime = (options&&options.withtime)||false;
50
51             if(dateformat=='rfc3339' && withtime) return m.format();
52
53             var timeformat = (options&&options.timeformat)||def_time_format;
54             var date_pattern = get_date_pattern(dateformat);
55             var time_pattern = !withtime?'':' '+get_time_pattern(timeformat);
56
57             return m.format(date_pattern+time_pattern);
58         }
59
60         window.$datetime = function(value, options) {
61             options = options||{};
62             options.withtime = true;
63             return $date(value, options);
64         };
65
66         window.$time = function(value, options) {
67             if(!value) return '';
68             var tz = (options&&options.tz)||def_tz;
69             var m = dayjs(value);
70             if(tz) m = m.tz(tz);
71
72             var dateformat = (options&&options.dateformat);
73             var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format;
74
75             return m.format(get_time_pattern(timeformat)+(dateformat=='rfc3339'?':ss'+(!m.isUTC()?'Z':''):''))+(dateformat=='rfc3339' && m.isUTC()?'Z':'');
76         }
77
78     })();
79 </script>
80 <!-- / js-date-format.inc -->