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

前端 未结 5 1199
無奈伤痛
無奈伤痛 2020-12-16 11:02

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

My javascript code is :



        
5条回答
  •  生来不讨喜
    2020-12-16 11:59

    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")
    

提交回复
热议问题