How to set ionic datetime default picking date without forcing it to be initially displayed?


#1

Hi,
Does anyone know how can I set the default value in datetime, like making it today forexample? Ionic sets it by default value/placeholder to the maximum date value which doesn’t make sense. I just want that when the user clicks to pick an option to find the default value/place holder that I set.

I tried binding the date to today’s date but then it automatically fills the date field with today’s date. But what if I want to leave it blank until the user chooses a date.

Thanks for your support


#2

Declare a variable in your component

this.startDate = new Date().toISOString();

and in your html

<ion-datetime
      displayFormat="MMM DD, YYYY HH:mm"
      [(ngModel)]="startDate"
       >
</ion-datetime>

#3

Thanks for your reply, but this doesn’t solve the problem as it forces today’s days to be displayed in the field by default… I would like to leave it blank until the user enters a date


#4

Ahh I misread it,
try this:

this.startDate = null;

#5

Again sets the default date in the picking options to the maximum :confused: It doesn’t show today’s date anymore…


#6

Hmmm I’m not sure how to handle this.

If I understood corectly, you want the value in the picker to be blank, but when you open the picker you want it to be set to todays date?


#7

Yup exactly that’s what I meant.
To todays date or to any value which makes sense but not automatically to the maximum date in 2030 forexample… then the user has to scroll down all the way. Just as an initial value to start scrolling from


#8

Hmm this functionality is related to this github issue.