Search code examples
javascriptglobalization

How not to show seconds with toLocaleString


https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString says that:

second The representation of the second. Possible values are "numeric", "2-digit".

What's the difference betweeen "numeric" and "2-digit"? What do I use to show nothing for seconds? I omitted the second option and it is still showing up.


Solution

  • 2-digit are 0 padded, whereas numeric is not. Not specifying the seconds (undefined) is how you omit them. However, you do need to specify that you want hours and seconds. Something like:

    var date = new Date();
    var options = { hour: 'numeric', minute: '2-digit' };
    console.log(date.toLocaleString('en-US', options));