0

I have user input field that accepts time value. Here is example of that value: 03:45:15.0 I would like to format this value with JavaScript to format the time like this: 03:35:15 PM. Is there a way to complete this with Vanilla JS?

Thank you.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • Why? its repeating logic. it already has 03: indicating before 12pm. in any case, you can do time = time + (Number(time.substring(0,2)) < 12 ? 'am' : 'pm') – Steve Tomlin Oct 22 '20 at 04:06
  • Are you using `input type="time"`? If so, that time likely represents AM, not PM, since that input type returns values in 24 hour time. – Heretic Monkey Oct 22 '20 at 04:08
  • @SteveTomlin Ok, what if I want in 24 hour format? Or if I want date plus time. – espresso_coffee Oct 22 '20 at 04:08
  • Does this answer your question? [How do you display JavaScript datetime in 12 hour AM/PM format?](https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format) – Heretic Monkey Oct 22 '20 at 04:09
  • 3
    Pick one - don't change the question mid-stream. – Randy Casburn Oct 22 '20 at 04:09
  • If you decide you want dates, then it's [How to format a JavaScript date?](https://stackoverflow.com/q/3552461/215552). There are basically already questions and answers for formatting dates and times for every format under the sun. – Heretic Monkey Oct 22 '20 at 04:12

0 Answers0