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 :: print to console without newline nodejs 
Javascript :: disable editing ace code edior 
Javascript :: get parameter from next.js route 
Javascript :: count the total number of digits of a number in javascript 
Javascript :: ajax download file 
Javascript :: js number to hex 
Javascript :: jquery for table Show entries 
Javascript :: NullInjectorError: R3InjectorError httpclient 
Javascript :: expo build android 
Javascript :: angular component between tags 
Javascript :: js shuffle array 
Javascript :: get variable name javascript 
Javascript :: performance.now nodejs example 
Javascript :: javascript show 2 decimal places 
Javascript :: pagecontrol 
Javascript :: javascript sort in array of objects 
Javascript :: how to find factorial of a number in javascript 
Javascript :: stop propagation event 
Javascript :: javascript multiply arguments 
Javascript :: validate name in javascript 
Javascript :: TypeError: date.getHours is not a function 
Javascript :: js create element 
Javascript :: unable to locate package npm 
Javascript :: js foreach querySelectorAll 
Javascript :: get unique values from array of objects javascript 
Javascript :: jquery insert option into select 
Javascript :: jmeter mac 
Javascript :: Ajax Form All Data Send 
Javascript :: how to execute javascript after c# function execute 
Javascript :: insta icon in next js 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =