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