# Ion-datetime binding through ngModel ionic-v4

**URL:** <https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187>\
**Category:** Ionic Framework\
**Created:** [August 2, 2018, 10:35pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187 "2018-08-02T22:35:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 2, 2018, 10:35pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/1 "2018-08-02T22:35:26Z")

</div>

i am binding through ngModel. but variable returning always undefined. i am using ionStyle event.

```auto
<ion-item class="itemdiv">
        <ion-label class="modtitle" position="floating">First Contact</ion-label>
        <ion-datetime display-format="DD/MM/YYYY" id="dat" [(ngModel)]="myDate" (ionStyle)="showdate()"></ion-datetime>
      </ion-item>

```

```auto
export class ModaldetailPage{
public myDate;

constructor( ) { }

showdate(){
    console.log(this.myDate);
  }

```

![33%20am](https://global.discourse-cdn.com/ionicframework/original/3X/5/1/516c47ad9ff222ddc81cd56f4f8e10bfdde32a76.png)

when i m changing variable to:

```auto
public myDate = new Date().toISOString();

```

then always getting current date. not changed value by picker.

 ![37%20am](https://global.discourse-cdn.com/ionicframework/original/3X/9/6/96d732539ed18a80d2419412678c42355a33d7e5.png)

![24%20am](https://global.discourse-cdn.com/ionicframework/original/3X/d/d/dd34a1ef7313bb5c42a6c14f5995f5ba0ce0fc7d.png)

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 3, 2018, 5:14am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/2 "2018-08-03T05:14:18Z")

</div>

Maybe same issue as the one not yet solved: [https://github.com/ionic-team/ionic/issues/14755](https://github.com/ionic-team/ionic/issues/14755)

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 3, 2018, 5:34am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/3 "2018-08-03T05:34:59Z")

</div>

Yes same issue. need to wait then i think.

---

<div class="post-metadata">

**Author:** ![haydencassar](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/haydencassar/32/118758_2.png) [@haydencassar](https://forum.ionicframework.com/u/haydencassar)\
**Post date:** [August 10, 2018, 5:34am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/4 "2018-08-10T05:34:29Z")

</div>

I think that issue was resolved in the latest release i think. DateTime now binds with ngModel for me.

But instead of getting an ISOString from the binding I’m getting an object with seperate properties for day, month, year, etc. I don’t know what to do with?

Also if you select that first value from a column (such as 01 day of the month) the value for that property is returned as null (eg month property is null).

Not sure if I’m doing something wrong, but somethings up.

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 10, 2018, 7:01am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/5 "2018-08-10T07:01:26Z")

</div>

> [@haydencassar](#):
>
> But instead of getting an ISOString from the binding I’m getting an object with seperate properties for day

For me I think it was already like this in v3. I implemented a `datetime` for the first time ever last week and this is the result I get

```
 const day: number = $event.day;
 const month: number = $event.month;
 const year: number = $event.year;
 
 const myDate = moment().year(year).month(month).date(day).toDate();

```

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 10, 2018, 5:33pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/6 "2018-08-10T17:33:53Z")

</div>

not working in V4. unable to extract day, month and year. can u check once? and in V4 doc of ionic said to use [https://date-fns.org/](https://date-fns.org/)

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 11, 2018, 7:16am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/7 "2018-08-11T07:16:45Z")

</div>

Doesn’t work for me neither, I have opened an issue in the Ionic repo [https://github.com/ionic-team/ionic/issues/15131](https://github.com/ionic-team/ionic/issues/15131)

I have also documented a workaround in the issue

P.S.: @anupzone about date-fns or moment, Ionic gives a recommendation, not a must. Both libs have their advantages and disadvantages, your call

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 11, 2018, 8:18am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/8 "2018-08-11T08:18:06Z")

</div>

perfect 🙂

ur work around is:

```auto
<ion-datetime #myPicker displayFormat='DD/MM/YYYY' (ionChange)="updateMyDate(myPicker.value)"></ion-datetime>

updateMyDate($event) {
  console.log($event); // --> wil contains $event.day.value, $event.month.value and $event.year.value
}

```

in V4 method is not “ionChange”, its “ionStyle”. change that. i got result:

 ![42%20pm](https://global.discourse-cdn.com/ionicframework/original/3X/8/9/893e58afdd3568a52a2e717f65289dbb2f37aa52.png)

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 11, 2018, 8:36am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/9 "2018-08-11T08:36:48Z")

</div>

yet but like I said in the issue, if user don’t select a value, like if he/she let the default “1” you don’t get “1” but you get null instead

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 11, 2018, 8:51am UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/10 "2018-08-11T08:51:54Z")

</div>

yes that’s the another issue. without event firing can’t get values.

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 19, 2018, 4:39pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/11 "2018-08-19T16:39:32Z")

</div>

is it resolved? still same.

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 19, 2018, 6:20pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/12 "2018-08-19T18:20:48Z")

</div>

[https://github.com/ionic-team/ionic/issues/15131](https://github.com/ionic-team/ionic/issues/15131) is solved

the following seems to work, not sure that’s the right way but seems to work

```
updateMyDate($event) {
  const day: number = $event.detail.value.day.value;
  const month: number = $event.detail.value.month.value;
  const year: number = $event.detail.value.year.value;
}
```

---

<div class="post-metadata">

**Author:** ![anupzone](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/anupzone/32/120703_2.png) [@anupzone](https://forum.ionicframework.com/u/anupzone)\
**Post date:** [August 20, 2018, 10:56pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/13 "2018-08-20T22:56:15Z")

</div>

i m using without $event fire. getting same json from id

```auto
(<HTMLInputElement>document.getElementById("picker")).value

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:26pm UTC](https://forum.ionicframework.com/t/ion-datetime-binding-through-ngmodel-ionic-v4/137187/14 "2021-07-19T21:26:19Z")

</div>

Closed on July 19, 2021, 9:26pm UTC


