4

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!

2

4 Answers 4

8

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.

2
  • what to do when its inside formGroup as a formControlName ?
    – Ankur Shah
    Commented Jun 26, 2018 at 11:44
  • @AnkurShah I think, this is not topic of this question. Maybe conside open a new question for this. Commented Jun 26, 2018 at 12:54
1

u can use this one too

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

and it will look like this:

01/06/2018
0

You can use the same methodology as

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

And in your ion-datetime do this instead:

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

I use the following format:

captura : any = new Date().toISOString();  
1
  • How is this any different from this answer from a year ago?
    – anothermh
    Commented Dec 26, 2018 at 22:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.