1

So I am working with an API connected to a MySQL Database with angular front-end but i have encountered a wall.

I want to access to the property startDate and deliveryDate, do some operations with Moment.js and finally change the color of a button with the difference in time.

Here is my api path and the return value

This is my Angular Service

 getStartDate(id:string): Observable<any>{
return this.http.get(`${this.API_URI}/tanks/startDate/${id}`)}

This is my Angular Component.ts, i get to here from a list of buttons, like the Hero tutorial on Angular Website

This is the method that returns an undefined but I need a string with the date "DD/MM/YYYY"

getStartDate(){
    const id = this.route.snapshot.paramMap.get('id');
    this.tankService.getStartDate(id)
    .subscribe(startDate => this.startDate = startDate);
    console.log(this.startDate)

  }

So that then i can do this which is simply get the difference between the two dates and return a string so i can change the color of a button.

getDuration(){
    var startDateMoment = moment(this.tank.startDate, "DD/MM/YYYY")
    var deliveryDateMoment = moment(this.tank.deliveryDate, "DD/MM/YYYY")
    console.log("Here")
    var diff = startDateMoment.diff(deliveryDateMoment, 'days')
    if (diff < 2 ) {
      this.semaphore = "danger"
    } else {
      if (diff > 4) {
        this.semaphore = "primary"
      } else{
        if (diff < 4 && diff > 2) {
          this.semaphore = "warning"
        }
      }
      
    }
  }

Maybe this is really simple but i can't get my head around it

Pasdian
  • 489
  • 1
  • 4
  • 7

0 Answers0