All files / src/app/common/format-AMPM format-AMPM.ts

100% Statements 10/10
87.5% Branches 7/8
100% Functions 3/3
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  1x 21x 4x   17x 17x       17x 17x 17x 17x       34x    
 
export function formatAMPM(date) {
  if (!date || !(date instanceof Date)) {
    return null;
  }
  const ampm = date.getHours() >= 12 ? 'pm' : 'am';
  return `${getHoursIn12HourFormat(date)}:${pad(date.getMinutes(), 2)}:${pad(date.getSeconds(), 2)} ${ampm}`;
}
 
function getHoursIn12HourFormat(date): number {
  let hours = date.getHours();
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  return hours;
}
 
function pad(num, size) {
  return ('000000000' + num).substr(-size);
}