Search code examples
javascripttime

How do I use .toLocaleTimeString() without displaying AM/PM?


How can I display a digital clock with the format "HH:MM" in Javascript?

The code below displays the digital clock in the format of "HH:MM AM/PM".

main.js

const d = new Date();
const cFormat = d.toLocaleTimeString([], {
      hour: "2-digit",
      minute: "2-digit",
    });

Edit: Although the code below works, I'd like to know if there is a way to do this via ".toLocaleTimeString()".

const d = new Date();
const cFormat = d.toLocaleTimeString('en-US', {hour: "2-digit",minute: "2-digit"});
cFormat.replace("AM", "").replace("PM", "");

console.log( 'cFormat -> ', cFormat )


Solution

  • this way

    const HH_MM = {hour: '2-digit', minute: '2-digit' }
    
    let cFormat = new Date()
                   .toLocaleTimeString('en-US', HH_MM)
                   .replace(/AM|PM/,'') 
    
    console.log( 'cFormat -> ', cFormat )