Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using your function (passing in date)
- function formatAMPM(date) {
- let days = date.getUTCDate();
- let month = date.getUTCMonth() + 1;
- const year = date.getUTCFullYear();
- let hours = date.getHours();
- let minutes = date.getMinutes();
- const ampm = hours >= 12 ? 'pm' : 'am';
- // converts hours to 12 hour instead of 24 hour
- hours = hours % 12;
- // converts 0 (midnight) to 12
- hours = hours ? hours : 12; // the hour '0' should be '12'
- // converts minutes to have leading 0
- minutes = minutes < 10 ? '0' + minutes : minutes;
- // converts minutes to have leading 0
- days = days < 10 ? '0' + days : days;
- month = month < 10 ? '0' + month : month;
- // the time string
- const time = hours + ':' + minutes + ' ' + ampm;
- //the result
- return `${days}/${month}/${year} ${time}`;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement