ionic 2 datetime display current date to future date and focused on today's date

19,406

Solution 1

The [(ngModel)]="date" is the right way to do it. Now you can declare following variable in your Typescript file:

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

This way, you creating a date, based on the actual time and formating it into a string that the <ion-datetime> component can work with. A proper string could look like this:

2017-07-23T09:10:19.621Z

In this case you do not need the time behind the date, because you only use the date.

Solution 2

u can use this one too

currentDate: string = new Date().toLocaleDateString();

and it will look like this:

01/06/2018
Share:
19,406
JSN
Author by

JSN

Web / Hybrid Mobile App Developer

Updated on July 12, 2022

Comments

  • JSN
    JSN almost 2 years

    I'm currently developing an app 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 is this => http://prntscr.com/fz5lx6

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

    How can I set that in ionic 2?

    Any help is much appreciated. Thanks!