0

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
Spilot
  • 1,495
  • 8
  • 29
  • 55
  • 2
    `new Date('2017-08-15').getTime()` – lux Aug 29 '17 at 19:42
  • You don't even need *getTime*: `new Date('2017-08-15') < Date.now()`, or if *listOfEventsObject[i].date* returns a Date object, `listOfEventsObject[i].date < Date.now()`. – RobG Aug 29 '17 at 22:36
  • Datepicker is implemented in such a twisted manner in javascript, any question regarding that is a boon to humankind.. Upvoting this question.. – Siddharth Mar 10 '18 at 06:54

1 Answers1

3

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);
spanky
  • 2,768
  • 8
  • 9
  • please see my updates – Spilot Aug 29 '17 at 20:36
  • 1
    @Spilot: There's not enough information in your question to know why that would be. – spanky Aug 29 '17 at 20:39
  • I'm not sure what else I can include. If anyone else has suggestions I still need them. – Spilot Aug 29 '17 at 20:44
  • 1
    We would need to see your data. Other than that, you'll need to solve this using typical debugging measure, which would at least include logging your values using `console.log()` to get a visual indication of why the comparisons are not giving the expected result. – spanky Aug 29 '17 at 20:53
  • 1
    There is no need for *getTime*, the less than operator coerces the Date to number. – RobG Aug 29 '17 at 22:37
  • @RobG: True, it'll call its `.valueOf()` method, which gives the same data. – spanky Aug 29 '17 at 23:23