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

前端 未结 16 1382
野性不改
野性不改 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:26

    function timeConversion(s) {
        let hour = parseInt(s.substring(0,2));
        hour = s.indexOf('AM') > - 1 && hour === 12 ? '00' : hour;
        hour = s.indexOf('PM') > - 1 && hour !== 12 ? hour + 12 : hour;
        hour = hour < 10 && hour > 0 ? '0'+hour : hour;
    
        return hour + s.substring(2,8);
    }
    

提交回复
热议问题