From: Coleman Watts Date: Tue, 17 Nov 2015 02:24:25 +0000 (-0500) Subject: Add CRM.utils.formatDate X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4cf9188ec3f2ea64189c0b8b397724d3fde2adec;p=civicrm-core.git Add CRM.utils.formatDate --- diff --git a/ang/crmStatusPage/StatusPage.html b/ang/crmStatusPage/StatusPage.html index 7517e50a91..5eefc509e0 100644 --- a/ang/crmStatusPage/StatusPage.html +++ b/ang/crmStatusPage/StatusPage.html @@ -18,7 +18,7 @@ {{status.title}}
- ({{status.hidden_until ? ts('Hidden until %1', {1: status.hidden_until}) : ts('Hidden permanently')}}) + ({{status.hidden_until ? ts('Hidden until %1', {1: formatDate(status.hidden_until)}) : ts('Hidden permanently')}})
diff --git a/ang/crmStatusPage/StatusPageCtrl.js b/ang/crmStatusPage/StatusPageCtrl.js index ed14b872d6..eba4413d22 100644 --- a/ang/crmStatusPage/StatusPageCtrl.js +++ b/ang/crmStatusPage/StatusPageCtrl.js @@ -2,20 +2,10 @@ angular.module('statuspage').controller('statuspageStatusPage', function($scope, crmApi, crmStatus, statusData) { - - function preprocessStatuses(apiData) { - _.each(apiData.values, function(status) { - if (status.hidden_until) { - var date = $.datepicker.parseDate('yy-mm-dd', status.hidden_until); - status.hidden_until = $.datepicker.formatDate(CRM.config.dateInputFormat, date); - } - }); - $scope.statuses = apiData.values; - } - preprocessStatuses(statusData); - $scope.ts = CRM.ts(); $scope.alert = CRM.alert; + $scope.formatDate = CRM.utils.formatDate; + $scope.statuses = statusData.values; // updates a status preference and refreshes status data $scope.setPref = function(status, until, visible) { @@ -30,7 +20,7 @@ ]; crmApi(apiCalls, true) .then(function(result) { - preprocessStatuses(result[1]); + $scope.statuses = result[1].values; }); }; diff --git a/js/Common.js b/js/Common.js index ed6f9d32d6..e7c237a446 100644 --- a/js/Common.js +++ b/js/Common.js @@ -1481,4 +1481,31 @@ if (!CRM.vars) CRM.vars = {}; var scale = Math.pow(10.0, len-digits); 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 ''; + } + switch (typeof input) { + case 'object': + // already a date object + break; + + case 'string': + // convert iso format + input = $.datepicker.parseDate('yy-mm-dd', input.substr(0, 10)); + break; + + case 'number': + // convert unix timestamp + input = new Date(input*1000); + break; + + default: + throw 'Invalid input passed to CRM.utils.formatDate'; + } + return $.datepicker.formatDate(outputFormat || CRM.config.dateInputFormat, input); + } })(jQuery, _);