1

I own a little game and my coder is not available. After an update he did, I have this error "date.getTime is not a function" in the console that causes crashes and I'm alone and very novice. I'd like you guys to please help me understand on how to solve it. Thanks

  hasPlayedInTheWeek(date) {
    var lastMonday = new Date();
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay()-1));
    lastMonday.setHours(0,0,0,0);

    const res = lastMonday.getTime() < date.getTime() && date.getTime() < ( lastMonday.getTime() + 604800000);
    return res;
  }
Dustin
  • 11
  • 3
  • Are you sure you are passing `date` as an arg to your function `hasPlayedInTheWeek` – Always Helping Jul 14 '20 at 04:46
  • How are you calling the function? Are you sure you are passing a `Date` object as the first argument? – Unmitigated Jul 14 '20 at 04:46
  • 1
    For starters, you are missing `function` keyword that should go just before `hasPlayedInTheWeek(date)` – shuberman Jul 14 '20 at 04:47
  • That being said, It'd be nice if you can post your .js file code. I'm sure you'll receive better help – shuberman Jul 14 '20 at 04:48
  • Just check if date is instance of Date if not then maybe return false: `if(!(date instanceof Date)){return false;}` first line of the function. – user3647971 Jul 14 '20 at 04:51
  • Thanks for the quick reply. Unfortunately I can't find it declared anywhere, he probably forgot and isn't replying right now. Is there a way for me to declare it ? – Dustin Jul 14 '20 at 04:52
  • @Dustin That function gets passed a parameter date which may or may not be instance of Date. You can search the file for "hasPlayedInTheWeek(" and see where it's called and with what parameters. But to get rid of the error you can just put the instanceof check I mention in previous comment to your function. – user3647971 Jul 14 '20 at 04:55
  • Thank you user3647971 and all for replying. @mishsx My js file is 700 lines long. Will it be a problem if I paste here ? – Dustin Jul 14 '20 at 05:06
  • @user3647971 I added if(!(date instanceof Date)){return false;} and it stopped working. Does this mean that date is an instance of Date ? – Dustin Jul 14 '20 at 05:14
  • Try logging `Object.prototype.toString.call(date)`. – Unmitigated Jul 14 '20 at 05:18
  • @ Dustin No It wont be a problem. – shuberman Jul 14 '20 at 05:31
  • @mishsx Thank you, following your advise I edited the post and posted a pastebin link to the whole Js file. Could you guys please have a look ? – Dustin Jul 14 '20 at 05:32
  • Okay so right off the bat I see an error: `Uncaught ReferenceError: require is not defined` Investigating more on the issue – shuberman Jul 14 '20 at 05:37
  • Did you check : https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined – shuberman Jul 14 '20 at 05:39

1 Answers1

0

add this before const res:

date = new Date(date);

code:

hasPlayedInTheWeek(date) {
    var lastMonday = new Date();
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay()-1));
    lastMonday.setHours(0,0,0,0);
    date = new Date(date)
    const res = lastMonday.getTime() < date.getTime() && date.getTime() < ( lastMonday.getTime() + 604800000);
    return res;
  }
AmirBll
  • 1,081
  • 1
  • 13
  • 25