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

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

    Make sure that your time is in this format HH:MM:SS(PM/AM)

    function timeConversion(s) {
    
        s = s.split(':');
        var time = s[2];
        if(time.charAt(2) === 'A' && parseInt(s[0]) == 12) s[0] = '00';
        if(time.charAt(2) === 'P' && parseInt(s[0]) <12) s[0] = parseInt(s[0])+12;
        if(s[0] >=24) s[0]-=24;
        var x = time.split('').slice(0,2);
        s[2] = x.join('');
        console.log(s.join(':'));
    }
    

提交回复
热议问题