2

I am trying to create a Time difference function. And I need to be able to tell if CookieTime is pasted. I have tried some many different things and just can't seem to get it to work. Any suggestions on why this wont subtract?

I was able to successfully do a DateDiff last night with help: Javascript DateDiff

The error in my console is: Uncaught TypeError: Object 12:34:00 has no method 'getTime' Don't know if I understand what that means.

CookieTime = "12:34:00"; //Cookie time...
currTime   = "05:11:55"; //Current Real Time...

function past(){

//Print the results for testing...
document.write(CookieTime + '<br>'); // = 12:34:00
document.write(currTime + '<br/>');  // = 05:11:55

// = Testing the results here = NaN
document.write(CookieTime.getTime() - currTime.getTime() + '<br/>');

    if (CookieTime - currTime >= 0){
      // Time has pasted!!
      return true;
    } else {
      // Time is not here yet!!
      return false;
    }
}

document.write(past()); //Print response.
Community
  • 1
  • 1
Frank G.
  • 1,519
  • 7
  • 25
  • 43

1 Answers1

7

You need to parse your original dates:

CookieTime = new Date('1970-1-1 12:34:00'); //Cookie time...
currTime   = new Date('1970-1-1 05:11:55'); //Current Real Time...
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
  • thanks for replying. What does that mean parse? Do I have to add an actual Date value to the time? Like you have it? – Frank G. Aug 18 '12 at 09:22
  • 1
    yes you need to form a real date object, as your are only dealing with time, the date part can be constant. you coud do: `CookieTime = new Date('1970-1-1 ' + somePreviouslyDefinedTimeStringVariable);` - the built-in javascript Date object has a constructor which accepts a string which gets parsed into a Date & Time representation – jenson-button-event Aug 18 '12 at 09:23
  • Do I also need to add that to the currTime value to? – Frank G. Aug 18 '12 at 09:26
  • 1
    yes. if you want to compare time, you need a date object, which needs a date part as well as a time part – jenson-button-event Aug 18 '12 at 09:27
  • okay going back to hack away any hopes I get this... Thanks again bud. Getting excited now because I have hope again haha! Thanks! – Frank G. Aug 18 '12 at 09:29
  • I'm getting an Invalid Date. This is what I tried `var currDate = currDate.getMonth() + 1 + "-" + currDate.getDate() + "-" + currDate.getFullYear();` `currTime = new Date(currDate + ' ' + currTime);` `document.write(currTime + '
    ');` = Invalid Date! Can you tell me what I am doing wrong here?
    – Frank G. Aug 18 '12 at 09:50
  • Okay I fixed it... I had to use & instead of + I guess it was trying to add them together. Thanks again @BobTodd – Frank G. Aug 18 '12 at 09:57
  • Can someone please tell me why this code `document.write(currDate + '
    '); //= 8-18-2012 document.write(CookieTime + '
    '); //= 8-18-2012 12:34:00 document.write(currTime + '
    '); //= 8-18-2012 05:59:49 CookieTime = new Date(currDate & ' ' & CookieTime); //Cookie time... currTime = new Date(currDate & ' ' & currTime); //Current Time.. ` Shows this as the value of both CookieTime and currTime: Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time) ?
    – Frank G. Aug 18 '12 at 10:02