1var suffix = hour >= 12 ? "PM":"AM";
2var hours = ((hour + 11) % 12 + 1) + suffix
1const formatAMPM = (date) => {
2 let hours = date.getHours();
3 let minutes = date.getMinutes();
4 const ampm = hours >= 12 ? 'pm' : 'am';
5
6 hours %= 12;
7 hours = hours || 12;
8 minutes = minutes < 10 ? `0${minutes}` : minutes;
9
10 const strTime = `${hours}:${minutes} ${ampm}`;
11
12 return strTime;
13};
14
15console.log(formatAMPM(new Date()));
16