Datetime component - show local time not UTC time?


#1

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>

#2

You need to process the timezone offset for this.


#3

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


#4

moment().format(); is correct


#5

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


#6

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.


Datetime in local time
#7

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


#8

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