daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //using your function (passing in date)
  2. function formatAMPM(date) {
  3.     let days = date.getUTCDate();
  4.     let month = date.getUTCMonth() + 1;
  5.     const year = date.getUTCFullYear();
  6.     let hours = date.getHours();
  7.     let minutes = date.getMinutes();
  8.     const ampm = hours >= 12 ? 'pm' : 'am';
  9.     // converts hours to 12 hour instead of 24 hour
  10.     hours = hours % 12;
  11.     // converts 0 (midnight) to 12
  12.     hours = hours ? hours : 12; // the hour '0' should be '12'
  13.     // converts minutes to have leading 0
  14.     minutes = minutes < 10 ? '0' + minutes : minutes;
  15.     // converts minutes to have leading 0
  16.     days = days < 10 ? '0' + days : days;
  17.     month = month < 10 ? '0' + month : month;
  18.  
  19.     // the time string
  20.     const time = hours + ':' + minutes + ' ' + ampm;
  21.  
  22.     //the result
  23.     return `${days}/${month}/${year} ${time}`;
  24. }
RAW Paste Data
Top