How do I convert this javascript date picker date to milliseconds?

1.2k Views Asked by At

I need to do a conditional where I test whether the date from a date picker has passed already. I need to compare it to todays date using Date.now(); which returns milliseconds.

when I my log date picker date from my object - listOfEventsObject[i].date - it prints in this form:

 **2017-08-15** . 

while date.now() outputs this: **1503904013430** 

how can I convert the date picker date in order to see if the time has passed?

UPDATE It does not work to use getTime(); when I use getTime(); it removes everything from the array. I only need passed dates left out of the array.

my code:

    function getPostsSuccess (listOfEventsObject){ /*callback which passes 
                                                             array to loop*/

        for (var i in listOfEventsObject) {

                if(listOfEventsObject[i].date.getTime() < Date.now()){

                       this.listOfEvents.push(listOfEventsObject[i]);

}//close if

                }//close loop



            }//close callback
1

There are 1 best solutions below

6
On

You can use the .getTime() method of the date object to get milliseconds that can be compared to Date.now().

var before = new Date(2000, 1, 1, 0, 0, 0, 0);

var now = Date.now();

var after = new Date(2100, 1, 1, 0, 0, 0, 0);

console.log("before is before now: %s", before.getTime() < now);

console.log("after is after now: %s", after.getTime() > now);