Datetime component - show local time not UTC time?

I am using the new datetime component for alarms, and I want it initialized to the current time. When I pass the component an ISO string, it just shows the UTC time instead of the local time. It is 10:28 pm local time, but it is showing 2:28 am (UTC).

this.time=  new Date().toISOString()

and

<ion-datetime displayFormat="h:mm a" pickerFormat="h mm a" [(ngModel)]="time"></ion-datetime>

You need to process the timezone offset for this.

1 Like

You can use this.time= moment.format();
It will automatically detect your timezone.

2 Likes

moment().format(); is correct

Error: While try to use that keyword Moment “Cannot find name ‘moment’”

Stop using moment. date-fns 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.

So you are saying that date-fns is able to do everything that moment does and better ?

Absolutely. You only get what you need, whereas moment is a monolithic behemoth.

In momentjs, we can…
receive “en-US” and “ll” and return “MMM D, YYYY”
receive “en-US” and “LL” and return “MMMM D, YYYY”
receive “id-ID” and “ll” and return “D MMM YYYY”
receive “id-ID” and “LL” and return “D MMMM YYYY”

Does date-fns also able to do this?

Thanks for the suggestion, You saved my day :slight_smile: