- // Format a date for output to the user
- // Input may be a js Date object, a unix timestamp or a yyyy-mm-dd string
- CRM.utils.formatDate = function(input, outputFormat) {
- return input ? $.datepicker.formatDate(outputFormat || CRM.config.dateInputFormat, CRM.utils.makeDate(input)) : '';
+ /**
+ * Format a date (and optionally time) for output to the user
+ *
+ * @param {string|int|Date} input
+ * Input may be a js Date object, a unix timestamp or a 'yyyy-mm-dd' string
+ * @param {string|null} dateFormat
+ * A string like 'yy-mm-dd' or null to use the system default
+ * @param {int|bool} timeFormat
+ * Leave empty to omit time from the output (default)
+ * Or pass 12, 24, or true to use the system default for 12/24hr format
+ * @returns {string}
+ */
+ CRM.utils.formatDate = function(input, dateFormat, timeFormat) {
+ if (!input) {
+ return '';
+ }
+ var date = CRM.utils.makeDate(input),
+ output = $.datepicker.formatDate(dateFormat || CRM.config.dateInputFormat, date);
+ if (timeFormat) {
+ var hour = date.getHours(),
+ min = date.getMinutes(),
+ suf = '';
+ if (timeFormat === 12 || (timeFormat === true && !CRM.config.timeIs24Hr)) {
+ suf = ' ' + (hour < 12 ? ts('AM') : ts('PM'));
+ if (hour === 0 || hour > 12) {
+ hour = Math.abs(hour - 12);
+ }
+ } else if (hour < 10) {
+ hour = '0' + hour;
+ }
+ output += ' ' + hour + ':' + (min < 10 ? '0' : '') + min + suf;
+ }
+ return output;