Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - date.getMonth is not a function

I'm using this Angular-2-datepicker. Everything works, however everytime I try to set the [(date)] attribute, it gives me the error.

date.getMonth is not a function

I've looked up some info and found out it could be because my variable is not of type date, however mine is, so I'm right now I'm stuck and cant find a solution.

My HTML:

...
<material-datepicker dateFormat="DD-MM-YYYY" weekStart="1" [(date)]="definitionDetails.From"></material-datepicker>
...

My Model:

export class ClosingDayDefinition implements Serializable<ClosingDayDefinition> {

    ID: number;
    DescriptionID: number;
    Info: models.DefinitionDetailInfo;
    Description: models.DefinitionDescription;
    CodeId: number;
    From: Date;
    To: Date;
    Recurring: boolean;
    Holiday: boolean;
    Groups: Array<models.ClosingDayGroup>;
    TypeName: string;

My Component:

...
public definitionDetails: models.ClosingDayDefinition;
...

Var Log:

enter image description here

like image 224
Nicolas Avatar asked Dec 19 '22 09:12

Nicolas


1 Answers

Seems like the From property contains a string, and not a Date object.

I'm guessing you're getting this date string from an API of some sort, try parsing it as a date instead of storing it straight away:

This will work

new Date('2017-03-08T00:00:00+01:00')
like image 95
Amit Avatar answered Dec 30 '22 22:12

Amit