Check a date input is greater than the systemDate ( dateTime.now) in ionic 2


#1

i have this input for the date i want to check if it’s greater than the actual date or not! please i need help

<ion-item>
    <ion-label color="appbase">Date</ion-label>
       <ion-datetime displayFormat="YYYY-MM-DD" min="2016" max="2020-10-31" 
       [(ngModel)]="datExpModel" formControlName="date_expiration"></ion-datetime>
   </ion-item>

#2

Hi @noutcharty1

Simple you to achieve your requirement is use Moment.js, with moment you can achieve any requirement for Date things, and for your greater than requirement please make use of isAfter() method of moment.

  1. Install via NPM:

npm install moment

2 - Import in your Typescript file:

import moment from ‘moment’;

3 - Use in your Typescript file:

setDate(getDate){
      var check = moment(getDate).isAfter();
      console.log(getDate, check);
}

4 - In your HTML:

<ion-item>
        <ion-label color="appbase">Date</ion-label>
        <ion-datetime displayFormat="YYYY-MM-DD" min="2016" max="2020-10-31" 
               [(ngModel)]="datExpModel" (ionChange)="setDate(datExpModel)"></ion-datetime>
</ion-item>

Please let us know, whether its meeting your requirement or not.


#3

I used to use moment, but it does not play nicely with dead code elimination. I would suggest date-fns isAfter is a better choice.


#4

it worked thank youuu


#5

i’ll test it thank youuu