ionic-frameworkionic2ionic3momentjsangular-calendar

How to get selected date from ionic-2 calendar onchange() method


I am using ion2-calendar.

this is my html:

 <ion-calendar [(ngModel)]="date"
              (onChange)="onChange($event)"
              [type]="type"
              [format]="'YYYY-MM-DD'">
</ion-calendar>

and this is onchange in ts:

onChange($event) {

      console.log("onchange event called");
      console.log(moment().format('DD-MM-YYYY'));  
    }

This is my console:

onSelect event called
25-06-2018

But i am always getting current month and year,no matter which date I choose. Only the date value is changing. Its showing current month and year in the console for all dates. Can someone tell me how to get selected date in dd-mm-yy format from $event object.

Edit- This is what i get for console.log($event)

enter image description here

Moment {_isAMomentObject: true, _i: 1530297000000, _isUTC: false, _pf: {…}, _locale: Locale, …}
_d
:
Sat Jun 30 2018 00:00:00 GMT+0530 (India Standard Time) {}
_i
:
1530297000000
_isAMomentObject
:
true
_isUTC
:
false
_isValid
:
true
_locale
:
Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", ordinal: ƒ, _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, …}
_pf
:
{empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__
:
Object

It shows correct date of click. It would be great if someone could tell me how to extract date from this .


Solution

  • $event of OnChange() is a moment object.

    you can format like this: $event.format('DD-MM-YYYY')