1

I have a method on visual studio code like this and when I run program, The program allows me to rent a product before the current time and I know new Date() method doesn't work on here how can I solve this?

 rent() {
        if (
          this.rentDate > this.returnDate ||
          this.rentDate <new Date()||
          !this.rentDate||
          !this.returnDate
        ) {
              this.toastrService.warning(
                  'Invalid dates'
              );
              return;
          }
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 1
    How do you know that `new Date()` "doesn't work" there? Can you clarify? – Ian H. Jun 18 '21 at 07:55
  • 2
    Date.now() https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Date/now – Rudy David Jun 18 '21 at 07:55
  • @IanH. because if it was be working it wouldn't let me to rent a product before the current time ? – Murat Akbyk Jun 18 '21 at 07:59
  • But how do you know that exactly that is the issue? Have you tried debugging to see what the other values contain? – Ian H. Jun 18 '21 at 08:04
  • 1
    You need to show how `this.rentDate` and `this.returnDate` are formatted. There are multiple formats a "date" could exist in Javascript. Possible duplicate: https://stackoverflow.com/q/492994/6513921 – ruth Jun 18 '21 at 08:05
  • @MichaelD `rentDate: Date ; returnDate: Date ;` – Murat Akbyk Jun 18 '21 at 08:09
  • @MuratAkbyk: Quickest way: Look into [`Date.getTime()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) and compare it with [`Date.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now). – ruth Jun 18 '21 at 08:12
  • @IanH. rentDate and returnDate are formatted as "Date" and when i try to rent after the returnDate it says invalid dates but when i try to rent before the current time it works – Murat Akbyk Jun 18 '21 at 08:14
  • @MichaelD i tried to compare with `Date.getTime()` but it said i can't use < operator with date and number types – Murat Akbyk Jun 18 '21 at 08:18
  • From the attached doc: "_The `getTime()` method returns the number of milliseconds* since the Unix Epoch_". So the comparison `this.rentTime.getTime() < Date.now()` would be a `number < number` comparison. Also note: I **DO NOT** if it take timezone offsets into account. – ruth Jun 18 '21 at 08:21
  • @MichaelD I did what you said it didn't show any errors but it didn't work – Murat Akbyk Jun 18 '21 at 08:34
  • @MuratAkbyk: Try to produce a minimal reproducible example. The data you've provided isn't enough to help. – ruth Jun 18 '21 at 08:55
  • **never** use javaScript to get the date if you're planning use in a dbs. You need get the DateTime of the server (the date in a local computer can be any) – Eliseo Jun 18 '21 at 11:35
  • `rentDate: Date` meas rentDate is the JavaScript Date object! – Salman A Jun 18 '21 at 11:39

2 Answers2

0

Date.now() creates a timestamp of now

new Date() creates a new date object of when it was created. On this object you can use the date methods (getDay, getMonth etc) as documented here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

James Trickey
  • 1,322
  • 13
  • 21
0

I solved the problem.

rentDate and returnDate were formatted as Date and i turned them into stringthen i write this code and it worked:

 if (
      Date.parse(this.rentDate) > Date.parse(this.returnDate) ||
      Date.parse(this.rentDate) <  Date.now()-86400000||
      !this.rentDate||
      !this.returnDate
   ) {
      this.toastrService.warning(
        "Invalid dates"
      );
      return;
    }