Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set my reactive form date input value?

So I am trying to display this date for an input. But it's not displaying when setting the value of it in my form builder group. I would also like to be able to format it too.

this.post.startDate is of type Date

Whats in .ts

this.editForm = this.formBuilder.group({
      startDate: [this.post.startDate, Validators.compose([Validators.required])],
      endDate: [this.post.endDate, Validators.compose([Validators.required])]
    });

My reactive form has this

<form [formGroup]="editForm" (ngSubmit)="saveDate()">

        <label>Start Date: </label>
        <input type="date" formControlName="startDate" name="startDate" />

        <br />
        <label>End Date: </label>
        <input type="date" formControlName="endDate" name="endDate" />

        <br /><br />
        <input type="submit" value="Create Date">

      </form>
like image 327
Jade Avatar asked Apr 12 '19 22:04

Jade


People also ask

How do I change the default date in Reactive form?

For setting default date value to <input> tag with type date you should use HTML-property value of input-tag. Variant 2 (recommended): You can use an built-in function formatDate() in angular.

How do I change value in Reactive form?

Setting or Updating of Reactive Forms Form Control values can be done using both patchValue and setValue. However, it might be better to use patchValue in some instances. patchValue does not require all controls to be specified within the parameters in order to update/set the value of your Form Controls.


2 Answers

You have two possibilities to set you date as default value.

Variant 1:

For setting default date value to <input> tag with type date you should use HTML-property value of input-tag.

Example:

<form [formGroup]="editForm2">
    <input type="date" formControlName="startDate" value="{{ post.startDate | date:'yyyy-MM-dd' }}">
    <input type="date" formControlName="endDate" value="{{ post.endDate | date:'yyyy-MM-dd' }}">
</form>

Variant 2 (recommended):

You can use an built-in function formatDate() in angular.

Step 1:

Import formatDate() from @angular/common in you component typescript file.

import { formatDate } from '@angular/common';

Note: formatDate(yourDate, format, locale) expects 3-4 parameters.

Step 2:

Format your dates in definition of your form group.

this.editForm = this.formBuilder.group({
      startDate: [formatDate(this.post.startDate, 'yyyy-MM-dd', 'en'), [Validators.required]],
      endDate: [formatDate(this.post.endDate, 'yyyy-MM-dd', 'en'), [Validators.required]]
});

Here is working example: https://stackblitz.com/edit/angular-kucypd

Here is documentation of input type date: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

like image 183
Gregor Doroschenko Avatar answered Sep 29 '22 00:09

Gregor Doroschenko


Try the built-in formatDate package.

import { formatDate } from '@angular/common' 

and then convert your date and set it to the datepicker control

this.yourForm.controls.controlName.setValue(formatDate(date,'yyyy-MM-dd','en'));
like image 40
Shashank Kapile Avatar answered Sep 28 '22 22:09

Shashank Kapile