File size: 1,604 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// @flow
export const convertTimestampToDate = (timestamp: number) => {
let monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
let date = new Date(timestamp);
let day = date.getDate();
let monthIndex = date.getMonth();
let month = monthNames[monthIndex];
let year = date.getFullYear();
let hours = date.getHours() || 0;
let cleanHours;
if (hours === 0) {
cleanHours = 12; // if timestamp is between midnight and 1am, show 12:XX am
} else {
cleanHours = hours > 12 ? hours - 12 : hours; // else show proper am/pm -- todo: support 24hr time
}
let minutes = date.getMinutes();
minutes = minutes >= 10 ? minutes : '0' + minutes.toString(); // turns 4 minutes into 04 minutes
let ampm = hours >= 12 ? 'pm' : 'am'; // todo: support 24hr time
return `${month} ${day}, ${year} at ${cleanHours}:${minutes}${ampm}`;
};
export const convertTimestampToTime = (timestamp: Date) => {
let date = new Date(timestamp);
let hours = date.getHours() || 0;
let cleanHours;
if (hours === 0) {
cleanHours = 12; // if timestamp is between midnight and 1am, show 12:XX am
} else {
cleanHours = hours > 12 ? hours - 12 : hours; // else show proper am/pm -- todo: support 24hr time
}
let minutes = date.getMinutes();
minutes = minutes >= 10 ? minutes : '0' + minutes.toString(); // turns 4 minutes into 04 minutes
let ampm = hours >= 12 ? 'pm' : 'am'; // todo: support 24hr time
return `${cleanHours}:${minutes}${ampm}`;
};
|