Ionic 2 datetime displays the current date on a future date and focuses on today's date

I am currently developing an application using ionic 2.

My problem is about datetime: I have this code in my html

  <ion-item>
    <ion-label stacked>Date</ion-label>
    <ion-datetime [(ngModel)]="date" formControlName="date" displayFormat="MMMM DD, YYYY" min="2017" max="2100"></ion-datetime>
  </ion-item>

      

The result will be => http://prntscr.com/fz5lx6

But I want to focus it on the current date, not the maximum date.

How can I set this in ionic 2?

Any help is greatly appreciated. Thank!

+3


source to share


2 answers


[(ngModel)]="date"

is the correct way to do it. Now you can declare the following variable in your Typescript file:

public date: string = new Date().toISOString();

      

So you create a date based on the actual time and shape it into a string that the component can handle <ion-datetime>

. The correct line might look like this:



2017-07-23T09:10:19.621Z

      

In this case, you don't need the time behind the date, because you are only using the date.

+4


source


You can use the same methodology as

myDate: String = new Date().toISOString();

      



And instead, in your ion-datetime, do this:

<ion-datetime displayFormat="HH:mm" pickerFormat="HH:mm" [(ngModel)]="myDate"></ion-datetime>

      

0


source







All Articles