0

I have a countup app. Here is the thing: https://stackblitz.com/edit/web-platform-mjepwa?file=main.js]

I have this code in my function. It sets from when the timer starts counting. past = Date.parse('2023-02-8 02:20:00'); I want to set the timer with "type="datetime-local" input.

I have no idea how to do it. Thanks in advance!

6
  • You can change your Date.parse(<date>) to use whatever the value of the input is. For example: var elem = document.querySelector("input.time-input"); var tm = elem.value; if(!tm) { tm = new Date(); tm = tm.toISOString(); } Is that what you had in mind? Commented Feb 10, 2023 at 22:34
  • @FiddlingAway—see Why does Date.parse give incorrect results? If the !tm test fails, then tm is a string and tm.toISOString() will throw an error. Hence answers should be as answers, not in comments. Commented Feb 13, 2023 at 5:00
  • Questions should include minimal code to demonstrate the issue, not reference code posted elsewhere. Commented Feb 13, 2023 at 5:03
  • @RobG Are you sure it will throw an error? Unless I'm misunderstanding you, this var tm = ""; if(!tm) { tm = new Date(); tm = tm.toISOString(); console.log(tm); } is showing what it's supposed to show: 2023-02-13T19:33:14.452Z at the time of posting this comment. Commented Feb 13, 2023 at 19:35
  • @FiddlingAway—the OP wants to use a time input, not datetime. If a value has been set, it will be like "19:33", which will be parsed as an invalid date and toISOString will throw a range error (or similar). Commented Feb 14, 2023 at 12:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.