Angular 2: How to use JavaScript Date Object with NgModel two way binding

后端 未结 7 1739
醉酒成梦
醉酒成梦 2020-12-02 10:01

I\'m working with Angular 2 and I have this code:

JS, this code initiates the employee-variable for the template:

handleEmployee(employee : Employee)         


        
7条回答
  •  执念已碎
    2020-12-02 10:14

    Fixed it with this code:

    handleEmployee(employee : Employee){
            this.employee = employee;
    
            let dateString : string = employee.startDate.toString();
            let days : number = parseInt(dateString.substring(8, 10));
            let months : number = parseInt(dateString.substring(5, 7));
            let years : number = parseInt(dateString.substring(0, 5));
            let goodDate : Date = new Date(years + "/" + months + "/" + days);
            goodDate.setDate(goodDate.getDate() + 2);
            this.date = goodDate.toISOString().substring(0, 10);
        }
    

    Html:

提交回复
热议问题