Issue
Here I am trying to develop a mobile application using an ionic framework. I want to set the current time and date in my application. But it didn't work. Can anyone help me to solve this problem? Here is my code.
HTML code
<ion-datetime
#dateTime
displayFormat="MMM DD, YYYY HH:mm"
formControlName="myDate"></ion-datetime>
ts. code
export class App{
@ViewChild('dateTime') dateTime;
form: FormGroup
myDate: FormControl = new FormControl('', Validators.required)
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.form = this.fb.group({
myDate: this.myDate
});
setTimeout(_ => {
this.dateTime.setValue(new Date().toISOString());
});
}
}
Solution
have you tried to use [value] property instead of ViewChild? Try to do this:
<ion-content>
<ion-datetime [value]="dateTime" displayFormat="MMM DD, YYYY HH:mm" ></ion-datetime>
</ion-content>
and in your ts file:
dateTime;
constructor() { }
ngOnInit() {
setTimeout(() => {
this.dateTime = new Date().toISOString();
});
}
this worked out for me!
Answered By - Jaime Lovera
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.