How to show only hours and minutes from javascript date.toLocaleTimeString()?
A more general version from @CJLopez's answer:
function prettyDate2(time) {
var date = new Date(parseInt(time));
return date.toLocaleTimeString(navigator.language, {
hour: '2-digit',
minute:'2-digit'
});
}
Original answer (not useful internationally)
You can do this:
function prettyDate2(time){
var date = new Date(parseInt(time));
var localeSpecificTime = date.toLocaleTimeString();
return localeSpecificTime.replace(/:\d+ /, ' ');
}
The regex is stripping the seconds from that string.
Here is a more general version of this question, which covers locales other than en-US. Also, there can be issues parsing the output from toLocaleTimeString(), so CJLopez suggests using this instead:
var dateWithouthSecond = new Date();
dateWithouthSecond.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'});
Use the Intl.DateTimeFormat library.
function prettyDate2(time){
var date = new Date(parseInt(time));
var options = {hour: "numeric", minute: "numeric"};
return new Intl.DateTimeFormat("en-US", options).format(date);
}