1

I set date_work to Date data type. But when I check the data type using the command console.log(typeof master.date_work) it appears for some reason the type of string. And because of this, when I use the getTime() function such an error occurs. How can I change the string data type to Date?

TypeError: master.date_work.getTime is not a function

Model:

export interface Masters {
    master_id?: string,
    full_name: string,
    comment: string,
    date_work: Date,
    teams_id: string
}

ts:

reports: Rep4Hours[]
masters: Masters[]
teams: Teams[]
filteredTeams = []
filteredMasters = []

onSelectedReport(reportId) {
  this.selectedReport = this.reports.find(
    el => {
      return el.report_id === reportId
    }
  )

  if (this.teamsService) {
    this.teamsService.fetch().subscribe(
      team => {
        this.teams = team
        this.filteredTeams = this.teams.filter(
          (team) => team.team_id == this.selectedReport.teams_id
        )
        if (this.mastersService) {
          this.mastersService.fetch().subscribe(
            master => {
              this.filteredMasters = []
              this.masters = master
              for(let team of this.filteredTeams){
                for(let master of this.masters){
                  if(master.teams_id == team.team_id){
                    if (Math.max(...this.masters.map(master => master.date_work.getTime()))) {
                      if (master.date_work <= this.selectedReport.report_date) {
                        this.filteredMasters.push(master)
                      }
                    }
                  }
                }
              }
            }
          )
        }
      }
    )
  }
}
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
user10720571
  • 353
  • 1
  • 4
  • 15
  • 4
    Your date is not a Date object and not of type Date. Defining a type of a property doesn't guarantee the same type to be assigned. Typescript will only check for compile time validations, what you assign at runtime is not validated. You can either convert all the `date_work` to date Object using `new Date(yourProp.date_work)`, or convert it only at the place you do the comparison – Ashish Ranjan Dec 26 '18 at 06:41
  • Please check master.date_work is not null – Ankur Shah Dec 26 '18 at 06:42
  • Just try loggin `this.masters` after the `this.masters = master;` line and check to see what is being logged on to the console. – SiddAjmera Dec 26 '18 at 06:50
  • Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Heretic Monkey Dec 26 '18 at 07:35

2 Answers2

0

Complementaring xyz's answer, your service can convert your "data_work" in Date Object of javaScript. (I supouse you received the date in string format)

//Your masterService
fetch()
{
     return this.httpClient.get(...)
     .pipe(map(x=>
           {...x,  //all the properties of "x"
            date_work:new Date(x.date_work)  //but date_work, convert to Date object 
           }
     ))
}
Eliseo
  • 50,109
  • 4
  • 29
  • 67
-1

your data date_work is not in Date format. Typescript cannot give guarantee for incoming service object.

there are inconsistencies between your model and service return object. When you map your class to returning object typescript annotates date_work as Date however in actual data it is not.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72