[removed] convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

前端 未结 16 1405
野性不改
野性不改 2020-11-30 03:35

The server is sending a string in this format: 18:00:00. This is a time-of-day value independent of any date. How to convert it to 6:00PM in Javasc

16条回答
  •  不知归路
    2020-11-30 04:12

    function timeformat(date1) {
      var date=new Date(date1);
      var month = date.toLocaleString('en-us', { month: 'long' });
      var mdate  =date.getDate();
      var year  =date.getFullYear();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var ampm = hours >= 12 ? 'pm' : 'am';
      hours = hours % 12;
      hours = hours ? hours : 12; // the hour '0' should be '12'
      minutes = minutes < 10 ? '0'+minutes : minutes;
      var strTime = mdate+"-"+month+"-"+year+" "+hours + ':' + minutes + ' ' + ampm;
      return strTime;
    }
    var ampm=timeformat("2019-01-11 12:26:43");
    console.log(ampm);
    

    Here the Function to Convert time into am or pm with Date,it may be help Someone.

提交回复
热议问题