-1

I don't quite know where I'm going wrong with respect to dates. I am getting the error:

"Argument of type 'string' is not assignable to parameter of type 'Date'. <button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create"

<mat-sidenav-content>
    <div class="sect">
        <h1>Damage Assessment Report</h1>
    </div>
    <mat-card>
        <mat-card-header>Create report:</mat-card-header>
        <br><br>
        <mat-card-content>
            <div> 
                <mat-form-field>
                    <mat-label>Report Description</mat-label>
                    <input
                        #DescOfReportInput  
                        placeholder="Description of Report"
                        matInput type= "string"
                        required>
                </mat-form-field>
                    <br>
                <mat-form-field>
                    <mat-label>Author</mat-label>
                    <input  
                        #AuthorInput
                        placeholder="Author"
                        matInput type= "string"
                        required>
                </mat-form-field>
                <br>
                <mat-form-field>
                <mat-label>Date</mat-label>
                <input  
                        #DateInput
                        placeholder="----/--/--"
                        matInput type= "Date"
                        required>
                </mat-form-field>
                <br>
            </div>
            <div> 
                <button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create</button>
            </div>
        </mat-card-content>
    </mat-card>
</mat-sidenav-content>

The associated TypeScript file is as follows:

import { Component, OnInit } from '@angular/core';
import { DamageAssessmentReportService } from 'src/app/damage-assessment-report.service';

@Component({
  selector: 'app-fa-daform',
  templateUrl: './fa-daform.component.html',
  styleUrls: ['./fa-daform.component.css']
})
export class FADAFormComponent implements OnInit {

    constructor(private damageAssessmentReportService : DamageAssessmentReportService) { }
  
    //assessmentDescription: string, author: string, reportDateTime: Date
  
    createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateTime: Date){
        this.damageAssessmentReportService
            .createDAReport(assessmentDescription,author, reportDateTime)
            .subscribe((response : any)=>{
                console.log(response);
            });
    }

    ngOnInit(): void { }
}    
Dillon
  • 134
  • 1
  • 5
  • 15
  • 3
    Your function expects a Date but you are giving a string for the last argument. – takendarkk Feb 04 '22 at 20:53
  • 1
    Just change `reportDateTime: Date` to `reportDateTime: string` – Kinglish Feb 04 '22 at 20:54
  • It works but created new problems when changing to `reportDateTime: string` when passing that into `this.damageAssessmentReportService .createDAReport(assessmentDescription,author, reportDateTime)` it gives the error _Argument of type 'string' is not assignable to parameter of type 'Date'_ where the variable reportDateTime is placed in the function parameters. – Dillon Feb 04 '22 at 21:12

1 Answers1

0

Just try this:

createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateString: string){

const reportDateTime = new Date(reportDateString); // Here you should have your date ready to be used as you wish

this.damageAssessmentReportService
   .createDAReport("New Report","Name", new Date(2020, 9, 10))
   .subscribe((response : any)=>{
                console.log(response);
   });
}