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

前端 未结 5 1195
無奈伤痛
無奈伤痛 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:49

    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.

    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'
      });
    }
    

提交回复
热议问题