0

I have a input type date at angular 4 and I have 2 field of Date picker, when I pick for example from 23.03.2018 to 29.03.2018 when I press save the date is taking the today date this is not changing, how it is possible this to happen. I tried with ngModelChange to change the value but in the database it is sending like the today date... Below you can find a code.

    <app-input-field orientation="top" label="Beginning Date">
        <input [min]="today" type="date" step="1" [(ngModel)]="newCreate"  style="width:250px">
      </app-input-field>
        <br>
        <app-input-field orientation="top" label="Ending Date">
          <input [min]="today" type="date" step="1" 
 [(ngModel)]="newEnd" style="width: 250px;">
        </app-input-field>

Here is the TS file

  newCreate: string = new Date().toISOString().substring(0, 19);
  newEnd: string = new Date().toISOString().substring(0, 19);

Here is when I click to save.

save() {

// How to convert here because when is saved it is saved like date 
//with minutes and everything else.
    this.newProject.name = this.newProjectName;
    this.newProject.state = this.newState;
    this.newProject.type = this.newType;
    this.newProject.created = new Date(this.newCreate);
    this.newProject.ending = new Date(this.newEnd);
    this.newProject.category = this.category;
    this.newProject.subProjectIds = this.subProjectIds;
    this.store.dispatch(new UpsertProjectAction(this.newProject));
    this.dialogRef.close(this.newProject);
  • You may wish to tag this with the library you're using. Different date picker components have different behavior. For instance, some require that the value be a `Date` object rather than a string. Also, you'll want to check [Converting string to date in js](https://stackoverflow.com/q/5619202/215552) – Heretic Monkey Mar 23 '18 at 15:57

1 Answers1

0

In your .ts file you're setting newCreate and newEnd to the current date.

If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings for timezone offset.

JavaScript - Date - MDN Docs

By using [(ngModel)] you're using two way data binding and on initialization your dates will be automatically set to the current date.

See this working StackBlitz: Working Demo

Narm
  • 10,677
  • 5
  • 41
  • 54
  • And how I can fix this do you have any idea? –  Mar 23 '18 at 15:57
  • Does the date picker return a string or a date? – Narm Mar 23 '18 at 16:02
  • I've tried recreating your problem in a StackBlitz, but it is working as expected. I can't reproduce your issue. Can you provide StackBlitz or code sample that will reproduce the issue? – Narm Mar 23 '18 at 16:13
  • This is all connected with interface and utils, at interface I have created: Date; and in the utils created: new Date() –  Mar 23 '18 at 16:19
  • Check out my working StackBlitz, did you define you properties like that? – Narm Mar 23 '18 at 16:22