Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setHours is not a function when converting time from string

I am trying to compare a time in string format to the current time. I've tried setting up two Date objects and calling .Now() on both of them, then on one of them adjusting the time to the time that is in string format by splitting it and parsing both the hours and minutes to integers, but I get the following error:

setHours is not a function

The 'cutoff' value I'm using is '15:00' and when following in the debugger I can see this splits in to split[0] = 15 and split[1] = 00 (this is before they are parsed into integers.

 var cutoff = data.CutOff;
 var split = cutoff.split(":");
 var today = Date.now();
 var hours = parseInt(split[0]);
 var min = parseInt(split[1]);
 today.setHours(hours, min);
 if (Date.now() < today) {
     // Do Something
 }
like image 287
Web Develop Wolf Avatar asked Dec 13 '22 20:12

Web Develop Wolf


1 Answers

You want to do new Date() as opposed to Date.now()

new Date creates a Date instance which allows you to access the Date methods.

Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

like image 182
Paul Fitzgerald Avatar answered Mar 06 '23 10:03

Paul Fitzgerald