The Date.prototype.toLocaleTimeString() method returns a string with a language-sensitive representation of the time portion of this date. It is available for modern browsers.
Unfortunately, the native function cannot prevent the output of seconds . By default, it displays the time format, for example hh:mm:ss or hh:mm AM/PM , etc.
second : representation of the second. Possible values: " numeric ", " 2-digit ".
Source: MDN Link
This means that you cannot use something like {second: false} .
I am looking for a simple stupid solution, remove seconds from the formatted string hh:mm:ss .
var date = new Date(); var time = date.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'}); console.log(time);
These regular expressions do not work :
time.replace(/:\d\d( |$)/,''); time.replace(/(\d{2}:\d{2})(?::\d{2})?(?:am|pm)?/);
javascript date regex
mate64
source share