Add parameter `showTwelveHours` to formatTime

pull/21833/head
Kieran Gould 2017-05-19 22:24:02 +01:00
parent 5aa1bc4185
commit f9152b205c
1 changed files with 5 additions and 6 deletions

View File

@ -16,7 +16,6 @@ limitations under the License.
'use strict'; 'use strict';
import UserSettingsStore from './UserSettingsStore';
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
@ -24,13 +23,13 @@ function pad(n) {
return (n < 10 ? '0' : '') + n; return (n < 10 ? '0' : '') + n;
} }
function twentyFourHour(date) { function twelveHourTime(date) {
let hours = date.getHours() % 12; let hours = date.getHours() % 12;
let minutes = pad(date.getMinutes()); let minutes = pad(date.getMinutes());
const ampm = hours >= 12 ? 'PM' : 'AM'; const ampm = hours >= 12 ? 'PM' : 'AM';
hours = pad(hours ? hours : 12); hours = pad(hours ? hours : 12);
minutes = pad(minutes); minutes = pad(minutes);
return hours + ':' + minutes + ' ' + ampm; return `${hours}:${minutes} ${ampm}`;
} }
module.exports = { module.exports = {
@ -54,9 +53,9 @@ module.exports = {
return days[date.getDay()] + ", " + months[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear() + " " + this.formatTime(date); return days[date.getDay()] + ", " + months[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear() + " " + this.formatTime(date);
}, },
formatTime: function(date) { formatTime: function(date, showTwelveHour=false) {
if (UserSettingsStore.getSyncedSetting('showTwelveHourTimestamps')) { if (showTwelveHour) {
return twentyFourHour(date); return twelveHourTime(date);
} }
return pad(date.getHours()) + ':' + pad(date.getMinutes()); return pad(date.getHours()) + ':' + pad(date.getMinutes());
}, },