Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const convertFrom24To12Format = (time24) => {
- const [sHours, minutes] = time24.match(/([0-9]{1,2}):([0-9]{2})/).slice(1);
- const period = +sHours < 12 ? 'AM' : 'PM';
- const hours = +sHours % 12 || 12;
- return `${hours}:${minutes} ${period}`;
- }
- const convertFrom12To24Format = (time12) => {
- const [sHours, minutes, period] = time12.match(/([0-9]{1,2}):([0-9]{2}) (AM|PM)/).slice(1);
- const PM = period === 'PM';
- const hours = (+sHours % 12) + (PM ? 12 : 0);
- return `${('0' + hours).slice(-2)}:${minutes}`;
- }
- function formatAMPM(date) {
- var hours = date.getHours();
- var minutes = date.getMinutes();
- var ampm = hours >= 12 ? 'pm' : 'am';
- hours = hours % 12;
- hours = hours ? hours : 12; // the hour '0' should be '12'
- minutes = minutes < 10 ? '0'+minutes : minutes;
- var strTime = hours + ':' + minutes + ' ' + ampm;
- return strTime;
- }
- console.log(formatAMPM(new Date));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement