Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

format time to am pm javascript

const formatAMPM = (date) => {
  let hours = date.getHours();
  let minutes = date.getMinutes();    
  const ampm = hours >= 12 ? 'pm' : 'am';

  hours %= 12;
  hours = hours || 12;    
  minutes = minutes < 10 ? `0${minutes}` : minutes;

  const strTime = `${hours}:${minutes} ${ampm}`;

  return strTime;
};

console.log(formatAMPM(new Date()));
Comment

how to convert time to am pm in javascript

var suffix = hour >= 12 ? "PM":"AM";
var hours = ((hour + 11) % 12 + 1) + suffix
Comment

PREVIOUS NEXT
Code Example
Javascript :: empty the value of an input in jquery 
Javascript :: jetbrains font 
Javascript :: sum an array in javascript 
Javascript :: javascript find unique values in array 
Javascript :: how to pronounce allele 
Javascript :: how to use Node.js Client for Google Maps Services for geolocation 
Javascript :: mongodb unshift array 
Javascript :: jquery is checked 
Javascript :: draw text in js 
Javascript :: scroll to bottom react 
Javascript :: dummy json 
Javascript :: how to filter an array of objects in javascript 
Javascript :: foreach jquery 
Javascript :: how to get aria expanded value in javascript 
Javascript :: select all checkboxes jquery 
Javascript :: chack var exist for skip error on javascript 
Javascript :: npm windows shocut 
Javascript :: add expiry to jwt extended token 
Javascript :: js set get first value 
Javascript :: scroll page to top after ajax success 
Javascript :: replace non alphanumeric javascript 
Javascript :: how to import jquery file in react js 
Javascript :: angular how to check previous route 
Javascript :: js detect hash change 
Javascript :: js ask before close chrome 
Javascript :: node download s3 file 
Javascript :: npm ERR! command /d /s /c node-pre-gyp install --fallback-to-build 
Javascript :: styled of styled component not working in nextjs 
Javascript :: js onchange paramteter sring 
Javascript :: javascript loop over classes 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =