40

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Venkaiah Yepuri
  • 1,561
  • 3
  • 18
  • 29
  • Does this answer your question? [How do I use .toLocaleTimeString() without displaying seconds?](https://stackoverflow.com/questions/17913681/how-do-i-use-tolocaletimestring-without-displaying-seconds) – kalley Sep 12 '22 at 14:01

5 Answers5

67

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'});
Community
  • 1
  • 1
Dan Cron
  • 1,105
  • 12
  • 24
44

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.

kalley
  • 18,072
  • 2
  • 39
  • 36
  • 5
    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. – Roshdy Aug 27 '17 at 12:11
  • This doesn't work for Finnish or German locales, as the separator symbol is "." (dot) not ":". @Dan Cron's answer is better. – Alonso Urbano Feb 04 '19 at 09:56
  • 1
    This is a bad answer, and should be deleted, for the reasons above. – Brad Nov 21 '21 at 02:32
6

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);
  } 
Mattias
  • 451
  • 3
  • 13
4

I posted my solution here https://stackoverflow.com/a/48595422/6204133

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

// '7.04 AM'

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
2

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")
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331