1

I want to display the current date as a default value,is not working if I use the formControlName but I need to use the formControlName.

Here is my HTML file

  <ion-item>
    <ion-label color="primary"> Start date </ion-label>
    <ion-datetime [value]="startDate.toISOString()" formControlName="startDate" required></ion-datetime>
  </ion-item>
  <div id="startDateErrorMessage" *ngIf="newProjForm.controls['startDate'].invalid &(newProjForm.controls['startDate'].dirty || newProjForm.controls['startDate'].touched)">
    <span class="error ion-padding" *ngIf="newProjForm.controls['startDate'].errors.required"> Date is required </span>
  </div>

and here is my TS file

import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
@Component({
selector: 'app-new-project',
templateUrl: './new-project.page.html',
styleUrls: ['./new-project.page.scss'],
})
export class NewProjectPage implements OnInit {
newProjForm: FormGroup;
startDate: Date = new Date();
constructor(private formBuilder: FormBuilder, private alertCtrl: AlertController) { }
ngOnInit() {
this.newProjForm = this.formBuilder.group({
  startDate: ['' ,[Validators.required]]
}); 
}
}

when I compile the project I'm getting this errors

"ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'toISOString' of undefined"

"TypeError: Cannot read property 'toISOString' of undefined"

Can anyone point me in the right direction please?

4
  • It means your value is coming as undefined. You can put a truthify check and avoid this error. You can access properties on undefined. startDate.toISOString() startDate seems to be undefined here. Commented Apr 30, 2021 at 4:41
  • To set the initial value in a form builder object, don't use [value] in the html template. You can replace the '' with the initial value of the field like: startDate: [(new Date()).toISOString(), [Validators.required]] Commented May 1, 2021 at 7:49
  • Thanks @C.Gäking now is working :) Commented May 2, 2021 at 17:47
  • Fine ;-) I'll write it as an answer again so that you can accept it, ok? I would be pleased Commented May 4, 2021 at 8:53

2 Answers 2

2

To set the initial value in a form builder object, don't use [value] in the html template. You can replace the '' with the initial value of the field like:

this.newProjForm = this.formBuilder.group({
  startDate: [(new Date()).toISOString(), [Validators.required]] –
});
Sign up to request clarification or add additional context in comments.

Comments

1

Error due to incorrect time display. The answer you're looking for:

// html:

<ion-datetime cancelText="отмена" doneText="Готово" [(ngModel)]="today" displayFormat="D MMM YYYY H:mm"
min="{{min_data}}" max="{{max_data}}" value="2021-11-15T11:00Z" monthShortNames="Январь, Февраль, Март, Апрель, Май, Июнь, Июль, Август, Сентябрь, Октябрь, Ноябрь, Декабрь">
</ion-datetime>

// ts:

today: any
min_data = ''
max_data = ''

pad(s, width, character) {
    return new Array(width - s.toString().length + 1).join(character) + s;
}

ngOnInit() {
    this.today = new Date()
    var numberOfDaysToAdd = 5 // increase by 5 days from today
    this.today.setDate(this.today.getDate() + numberOfDaysToAdd)
    this.min_data = this.today.getFullYear()+'-'+this.pad(this.today.getMonth() + 1, 2, '0')+'-'+this.pad(this.today.getDate(), 2, '0')
    this.max_data = (this.today.getFullYear()+1)+'-'+this.pad(this.today.getMonth() + 1, 2, '0')+'-'+this.pad(this.today.getDate(), 2, '0')
    this.today = this.today.getFullYear()+'-'+this.pad(this.today.getMonth() + 1, 2, '0')+'-'+this.pad(this.today.getDate(), 2, '0') + ' 18:00'
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.