Rekha
Rekha

Reputation: 423

How to Compare the future date with current date in angular?

Component.ts(Here I would like to compare the date from today greater than expiry date.. I have tried this code.. but it's not working as expected.. could you please help me out??)(Another thing i would like to get the valueofcertificate map into the getAgents array then based on condition i need to change the value of valueofcertificate variable..its is not working properly.. could you please find any mistakes in my code?? thank you in advance)

      getAllAgents() {
    this.agentShowSpinner = true;
    // this.agentSuccessSubscription.unsubscribe()
    // this.agentErrorSubscription.unsubscribe()
    this.store.dispatch(new GetAllAgents(`${this.userService.getUserdetails().CompanyUser.company_id.id}/true`))
    this.agentSuccessSubscription = this.store.pipe(select(getAllAgentsSuccess)).subscribe((result : any) => {
      if(!!result) {
        this.getAgents = result
        this.getAgents.map(item => item.valueofCertificate = '')
        this.getAgents.forEach(element =>{

          if(element.foodsafetycertificate.length == 0){
            console.log(element.foodsafetycertificate.length)
              element.valueofCertificate = "Certificates Not Available"
              console.log(element.valueofCertificate )
          }
          else{
           element.valueofCertificate = this.getFoodCertificateValues(element)
      }


        })
        console.log(this.getAgents)
        this.dataSource = new MatTableDataSource(result)
        this.dataSource.paginator = this.paginator;
      }  else {
        this.dataSource = new MatTableDataSource([])
        this.dataSource.paginator = this.paginator;
      }
      this.agentShowSpinner = false
    })
    this.agentErrorSubscription =  this.store.pipe(select(getAllAgentsError)).subscribe((result : any) => {
      if(!!result) {
        alert(result)
        this.agentShowSpinner = false
      }
    })
  }

  getFoodCertificateValues(element: any){// those conditions are not working.. could you help me out?

    let now = new Date
    now.setHours(0,0,0,0);
    element.foodsafetycertificate.forEach(item => {
      if(!item.expiry_date){
        element.valueofCertificate = "Certificates Not Available"
      }
      else if(item.expiry_date  >= new Date(now.setDate(now.getDate()))){
        element.valueofCertificate = "Certificates are Valid"
          }
       else if(item.expiry_date <= new Date(now.setDate(now.getDate()))){
        element.valueofCertificate = "Certificates Expired"
          }
    })


  }

Upvotes: 2

Views: 8381

Answers (1)

Sahil Ralkar
Sahil Ralkar

Reputation: 2424

check the following simple solution for two date comparison

let d = new Date();
var g1 = new Date(d.getFullYear(), d.getMonth(), d.getDate());
// (YYYY, MM, DD) 
var g2 = new Date(2021, 08, 03);
if (g1.getTime() < g2.getTime())
    document.write("g1 is lesser than g2");
else if (g1.getTime() > g2.getTime())
    document.write("g1 is greater than g2");
else
    document.write("both are equal");

Upvotes: 3

Related Questions