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