2015-11-30 19:11:04 +01:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-30 19:11:04 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-05-18 23:00:44 +02:00
|
|
|
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
|
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
2015-11-30 19:11:04 +01:00
|
|
|
|
2017-05-15 01:25:12 +02:00
|
|
|
function pad(n) {
|
|
|
|
return (n < 10 ? '0' : '') + n;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:02 +02:00
|
|
|
function twelveHourTime(date) {
|
2017-05-19 22:31:24 +02:00
|
|
|
let hours = date.getHours() % 12;
|
|
|
|
let minutes = pad(date.getMinutes());
|
|
|
|
const ampm = hours >= 12 ? 'PM' : 'AM';
|
|
|
|
hours = pad(hours ? hours : 12);
|
|
|
|
minutes = pad(minutes);
|
2017-05-19 23:24:02 +02:00
|
|
|
return `${hours}:${minutes} ${ampm}`;
|
2017-05-18 23:00:44 +02:00
|
|
|
}
|
|
|
|
|
2015-11-30 19:11:04 +01:00
|
|
|
module.exports = {
|
|
|
|
formatDate: function(date) {
|
|
|
|
var now = new Date();
|
|
|
|
if (date.toDateString() === now.toDateString()) {
|
2017-05-18 23:00:44 +02:00
|
|
|
return this.formatTime(date);
|
2015-11-30 19:11:04 +01:00
|
|
|
}
|
|
|
|
else if (now.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
|
2017-05-18 23:00:44 +02:00
|
|
|
return days[date.getDay()] + " " + this.formatTime(date);
|
2015-11-30 19:11:04 +01:00
|
|
|
}
|
2017-05-15 01:25:12 +02:00
|
|
|
else if (now.getFullYear() === date.getFullYear()) {
|
2017-05-18 23:00:44 +02:00
|
|
|
return days[date.getDay()] + ", " + months[date.getMonth()] + " " + date.getDate() + " " + this.formatTime(date);
|
2015-11-30 19:11:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-05-15 01:25:12 +02:00
|
|
|
return this.formatFullDate(date);
|
2015-11-30 19:11:04 +01:00
|
|
|
}
|
2017-05-15 01:25:12 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
formatFullDate: function(date) {
|
2017-05-18 23:00:44 +02:00
|
|
|
return days[date.getDay()] + ", " + months[date.getMonth()] + " " + date.getDate() + " " + date.getFullYear() + " " + this.formatTime(date);
|
2016-08-16 16:01:01 +02:00
|
|
|
},
|
|
|
|
|
2017-05-19 23:24:02 +02:00
|
|
|
formatTime: function(date, showTwelveHour=false) {
|
|
|
|
if (showTwelveHour) {
|
|
|
|
return twelveHourTime(date);
|
2017-05-18 23:00:44 +02:00
|
|
|
}
|
2017-05-15 01:25:12 +02:00
|
|
|
return pad(date.getHours()) + ':' + pad(date.getMinutes());
|
2017-05-18 23:00:44 +02:00
|
|
|
},
|
2017-01-20 15:22:27 +01:00
|
|
|
};
|