How to show only hours and minutes from javascript date.toLocaleTimeString()?
Asked Answered
C

6

42

Can anyone please help me get the HH:MM am/pm format instead of HH:MM:SS am/pm.

My javascript code is :

function prettyDate2(time){
  var date = new Date(parseInt(time));
  var localeSpecificTime = date.toLocaleTimeString();
  return localeSpecificTimel;
} 

It returns the time in the format HH:MM:SS am/pm, but my client's requirement is HH:MM am/pm.

Please help me.

Chloric answered 16/10, 2013 at 15:19 Comment(1)
Does this answer your question? How do I use .toLocaleTimeString() without displaying seconds?Mulciber
M
45

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.

Mulciber answered 16/10, 2013 at 15:24 Comment(3)
bear in mind that for non english locale, this solution won't work, as the regex of \d won't detect the numbers. Example: date.toLocaleTimeString('ar'). Therefore, @Dan Cron 's answer is better for general use.Gaffney
This doesn't work for Finnish or German locales, as the separator symbol is "." (dot) not ":". @Dan Cron's answer is better.Belsen
This is a bad answer, and should be deleted, for the reasons above.Zaporozhye
L
68

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'});
Listen answered 23/9, 2015 at 13:48 Comment(0)
M
45

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.

Mulciber answered 16/10, 2013 at 15:24 Comment(3)
bear in mind that for non english locale, this solution won't work, as the regex of \d won't detect the numbers. Example: date.toLocaleTimeString('ar'). Therefore, @Dan Cron 's answer is better for general use.Gaffney
This doesn't work for Finnish or German locales, as the separator symbol is "." (dot) not ":". @Dan Cron's answer is better.Belsen
This is a bad answer, and should be deleted, for the reasons above.Zaporozhye
V
7

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);
  } 
Vogele answered 16/10, 2013 at 15:30 Comment(0)
S
6

I posted my solution here https://mcmap.net/q/165535/-how-to-convert-time-in-milliseconds-to-hours-min-sec-format-in-javascript

var textTime = new Date(sunriseMills + offsetCityMills + offsetDeviceMills) 
                .toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' });

// '7.04 AM'

Sycee answered 3/2, 2018 at 9:4 Comment(0)
C
1

You may also try like this:-

function timeformat(date) {
  var h = date.getHours();
  var m = date.getMinutes();
  var x = h >= 12 ? 'pm' : 'am';
  h = h % 12;
  h = h ? h : 12;
  m = m < 10 ? '0'+m: m;
  var mytime= h + ':' + m + ' ' + x;
  return mytime;
}

or something like this:-

new Date('16/10/2013 20:57:34').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
Coach answered 16/10, 2013 at 15:24 Comment(0)
S
0

One (long) line function in Typescript:

const prettyDate2 = (time : number ) => new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: 'numeric' }).format(new Date(time));

Notice that some of the above solutions with toLocaleTimeString will also show the date, and Intl.DateTimeFormat is officially more recommended.

Shot answered 3/6 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.