# Datetime in local time

**URL:** <https://forum.ionicframework.com/t/datetime-in-local-time/61733>\
**Category:** ionic-v3\
**Created:** [August 26, 2016, 6:55pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733 "2016-08-26T18:55:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensengar](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jensengar](https://forum.ionicframework.com/u/jensengar)\
**Post date:** [August 26, 2016, 6:55pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/1 "2016-08-26T18:55:09Z")

</div>

How can I show the time in local time? For example, I would expect `new Date().toISOString()` to show the current time relative to me, not the utc time.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 26, 2016, 9:31pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/2 "2016-08-26T21:31:16Z")

</div>

Use [moment](http://momentjs.com/). It’s far better than JavaScript’s `Date`s.

---

<div class="post-metadata">

**Author:** ![jensengar](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jensengar](https://forum.ionicframework.com/u/jensengar)\
**Post date:** [August 27, 2016, 10:29am UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/3 "2016-08-27T10:29:52Z")

</div>

I used `new Date()` just to make my question simple, but I have to same problem if I do `moment().toISOString()`. Right now, it’s 6:30am but the datetime says 10:30am.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 28, 2016, 8:22pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/4 "2016-08-28T20:22:04Z")

</div>

> [@jensengar](#):
>
> I have to same problem if I do moment().toISOString()

`toISOString` deliberately uses UTC. Try `format`.

---

<div class="post-metadata">

**Author:** ![frl](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/frl/32/105293_2.png) [@frl](https://forum.ionicframework.com/u/frl)\
**Post date:** [December 10, 2016, 9:31am UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/5 "2016-12-10T09:31:17Z")

</div>

My scenary odata services,return datetimes as ISO 8601 UTC  
The dates are declared in my model’s interfaces  
`DateProperty:Date`

Model object (for sample only one Date field used)

```
{ExpectedEndDate:"2016-12-01T04:00:00Z"}

```

I use pipe to show in primeng DataTable, yes, i merge it with ionic2

```
<p-column field="ExpectedEndDate" header="F.Prevista">
            <template let-col let-item="rowData" pTemplate type="body">
                <span>{{item[col.field] | date: 'dd/MM/yyyy HH:mm:ss'}}</span>
            </template>
</p-column>

```

this show the date in my local time as expected (GMT+0100)

```
01/12/2016 05:00:00

```

Using Ionic DateTime over my model’s property, show the edited date as UTC ☹  
for solve i do this in my form.ts  
Declare 2 functions (i presume can be some kind of global)

```
setIonicDateTime(value: string): Date {
    if (value) {
        let date: Date = new Date(value);
        let ionicDate = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
        return ionicDate;
    }
    return null;
}
getIonicDateTime(value: Date): string {
    if (value) {
        let date: Date = new Date(value);
        let ionicDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()));
        return ionicDate.toISOString();
    }
    return null;
}

```

then wrap my model propery (edit is my model)

```
@Input() set ExpectedEndDate(value: string) {
    this.edit.ExpectedEndDate = this.setIonicDateTime(value);
}
get ExpectedEndDate():string {
   return this.getIonicDateTime(this.edit.ExpectedEndDate);
}

```

in the template the code for for the date use the property instead of model one.

```
<ion-item>
    <ion-label floating>F.Prevista</ion-label>
    <ion-datetime formControlName="ExpectedEndDate" displayFormat="DD/MM/YYYY HH:mm" [(ngModel)]="ExpectedEndDate">
    </ion-datetime>
</ion-item>

```

This way works fine for me, allways show and edit the dates in local time and store in UTC.  
this work allways the dates are ISO 8601 UTC

---

<div class="post-metadata">

**Author:** ![gusgonnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gusgonnet/32/121606_2.png) [@gusgonnet](https://forum.ionicframework.com/u/gusgonnet)\
**Post date:** [September 2, 2018, 4:28am UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/6 "2018-09-02T04:28:52Z")

</div>

> [@rapropos](#):
>
> Use [moment](http://momentjs.com/). It’s far better than JavaScript’s `Date` s.

Naturally things change with time. Perhaps you’d like to update that post above for future visitors?

> [@Datetime component - show local time not UTC time?](https://forum.ionicframework.com/t/datetime-component-show-local-time-not-utc-time/54171/6):
>
> Stop using moment. [date-fns](https://date-fns.org/) is much lighter and can work with either native Date objects or ISO8601 strings directly, instead of needing a dedicated object that is hard to work with.

As an alternative, I ended up using Angular’s DatePipe:

> **[Angular](https://angular.io/api/common/DatePipe)**
>
> Angular is a platform for building mobile and desktop web applications.
> Join the community of millions of developers who build compelling user interfaces with Angular.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 4, 2018, 6:28pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/7 "2018-09-04T18:28:40Z")

</div>

> [@gusgonnet](#):
>
> Perhaps you’d like to update that post above for future visitors?

I would love to, but Discourse (the software that powers the forums) locks you out from editing after a certain timeframe.

---

<div class="post-metadata">

**Author:** ![gusgonnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gusgonnet/32/121606_2.png) [@gusgonnet](https://forum.ionicframework.com/u/gusgonnet)\
**Post date:** [September 4, 2018, 7:31pm UTC](https://forum.ionicframework.com/t/datetime-in-local-time/61733/8 "2018-09-04T19:31:08Z")

</div>

oh, right, I forgot about that feature (that I hate a bit).  
Thanks!
