Time Conversion

  • + 0 comments

    I think one approrach to use Javascript string split.

        const [time, period] = time12h.split(/(AM|PM)/);
        const [hours, minutes, seconds] = time.split(':');
        const hours24h = period === 'PM' && hours !== '12' ? parseInt(hours) + 12 : hours === '12' && period === 'AM' ? '00' : hours;
        return `${hours24h}:${minutes}:${seconds}`;