From 3c68de9dfb5b3dd19c10b0ef388fd10b4bdebd65 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 17 Nov 2015 11:10:47 -0500 Subject: [PATCH] Add CRM.utils.makeDate function --- js/Common.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/js/Common.js b/js/Common.js index a3a43a6f6b..5abee08f67 100644 --- a/js/Common.js +++ b/js/Common.js @@ -1482,30 +1482,27 @@ if (!CRM.vars) CRM.vars = {}; return Math.round(n / scale) * scale; }; - // 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) { - if (!input) { - return ''; - } + // Create a js Date object from a unix timestamp or a yyyy-mm-dd string + CRM.utils.makeDate = function(input) { switch (typeof input) { case 'object': // already a date object - break; + return input; case 'string': // convert iso format - input = $.datepicker.parseDate('yy-mm-dd', input.substr(0, 10)); - break; + return $.datepicker.parseDate('yy-mm-dd', input.substr(0, 10)); case 'number': // convert unix timestamp - input = new Date(input*1000); - break; - - default: - throw 'Invalid input passed to CRM.utils.formatDate'; + return new Date(input * 1000); } - return $.datepicker.formatDate(outputFormat || CRM.config.dateInputFormat, input); + throw 'Invalid input passed to CRM.utils.makeDate'; + }; + + // 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)) : ''; }; })(jQuery, _); -- 2.25.1