How to format time hours-minutes-seconds in Node.js

1 Answer

0 votes
// Get the current time
const currentTime = new Date();

// Format the time as "HH:MM:SS AM/PM"
const formattedTime = currentTime.toLocaleTimeString('en-US', {
    hour12: true,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
});

console.log("Formatted Time:", formattedTime);


 

/*
run:
  
Formatted Time: 06:26:53 PM
     
*/
 


 



answered Jul 22 by avibootz

Related questions

...