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!

  • You can change your `Date.parse()` 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? – FiddlingAway Feb 10 '23 at 22:34
  • @FiddlingAway—see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results?r=SearchResults&s=1%7C625.0350) 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. – RobG Feb 13 '23 at 05:00
  • Questions should include minimal code to demonstrate the issue, not reference code posted elsewhere. – RobG Feb 13 '23 at 05: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. – FiddlingAway Feb 13 '23 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). – RobG Feb 14 '23 at 12:02
  • @RobG *I want to set the timer with "type="datetime-local" input.* - This is in the OP's question. And in the stackblitz, this is the input: ``. When the value of that input is set (for example, to today), and when the time is entered manually (typing in the field), the value becomes something like `2023-02-16T11:11`. When this is processed like so: `var tm = Date.parse('2023-02-16T11:11'); tm.toISOString();` the output is `"2023-02-16T10:11:00.000Z"`. So, again, no error there. – FiddlingAway Feb 14 '23 at 20:26

0 Answers0