Bug 24455: (follow-up) Document function
[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 <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             var tz = (options&&options.tz)||def_tz;
35             var m = moment(value);
36             if(tz) m.tz(tz);
37
38             var dateformat = (options&&options.dateformat)||def_date_format;
39             var withtime = (options&&options.withtime)||false;
40
41             if(dateformat=='rfc3339' && withtime) return m.format();
42
43             var timeformat = (options&&options.timeformat)||def_time_format;
44             var date_pattern = get_date_pattern(dateformat);
45             var time_pattern = !withtime?'':' '+get_time_pattern(timeformat);
46
47             return m.format(date_pattern+time_pattern);
48         }
49
50         window.$datetime = function(value, options) {
51             options = options||{};
52             options.withtime = true;
53             return $date(value, options);
54         };
55
56         window.$time = function(value, options) {
57             var tz = (opitons&&options.tz)||def_tz;
58             var m = moment(value);
59             if(tz) m.tz(tz);
60
61             var dateformat = (options&&options.dateformat);
62             var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format;
63
64             return m.format(get_time_pattern(timeformat)+(dateformat=='rfc3339'?':ss'+(!m.isUTC()?'Z':''):''))+(dateformat=='rfc3339' && m.isUTC()?'Z':'');
65         }
66
67     })();
68 </script>