const digits = ['midnight','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty ']; const keys = ["o'clock", "quarter", "half"] const timeToWord = (time) => { let num = time.split(':').map((t, i) => (i === 0 && t !== '12') ? +t % 12 : +t); let word = ''; if (!num[0] && !num[1]) word = digits[0]; else if(num[0] && !num[1]) word = digits[num[0]] + " " + keys[0]; else { if (num[1] <= 30) { if (num[1] % 15 === 0) word = keys[num[1] / 15] + " past " + digits[num[0]]; else word = (num[1] > 20 ? (digits[20] + digits[num[1] - 20]) : digits[num[1]]) + (num[1] > 1 ? " minutes " : " minute ") + "past " + digits[num[0]]; } else { num[1] = 60 - num[1]; num[0] += 1; if (num[0] === 12) num[0] = (+time.split(':')[0] > 12 ? 0 : 12) if (num[1] % 15 === 0) word = keys[num[1] / 15] + " to " + digits[num[0]]; else word = (num[1] > 20 ? (digits[20] + digits[num[1] - 20]) : digits[num[1]]) + (num[1] > 1 ? " minutes " : " minute ") + "to " + digits[num[0]]; } } console.log(time + " = " + word); return word; } timeToWord('13:00'); timeToWord('13:09'); timeToWord('13:15'); timeToWord('13:29'); timeToWord('13:30'); timeToWord('13:31'); timeToWord('13:45'); timeToWord('00:48'); timeToWord('00:08'); timeToWord('12:00'); timeToWord('00:00'); timeToWord('07:01'); timeToWord('01:59'); timeToWord('12:01'); timeToWord('00:01'); timeToWord('11:31'); timeToWord('23:31');
A little big but does the job
Output
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A little big but does the job
Output