Ion-datetime showing not local time


#1

Hi friends,
I used ion-datetime but the selector shows the UTC time. I need to show my local time.

<ion-datetime [(ngModel)]="data" displayFormat="DD/MM/YYYY HH:mm" class="data-box"></ion-datetime>

I set “data” variable in my constructor. The string is local time but I show the UTC format.

Example. The time in Italy is 23:51. I set this.data = 03/07/2018 23:51 but when the input field will apear, I see 23:51

Support me please

Luca


#2

Your post is confusing.

That sounds like you are seeing the time you want, no? ion-datetime doesn’t care about timezones; it just deals with ISO8601 strings, so if you want the time and date you described, you just say:

this.data = "2018-07-03T23:51";

#3

@lsantaniello… use moment js plugin

//install using command

>npm i moment

//ts
import * as moment from 'moment';
this.data = moment().format();

#4

The advice in the previous post will needlessly bloat your app binary significantly.


#5

What is the alternative?


#6

@rapropos is a big fan of date-fns so I guess that’s the alternative, feel free to correct me If im wrong :slight_smile:


#7

I integrated moment. Can I use Pipe into my html file?

for example: {{p.data | moment:‘dd/MM HH:mm’}}

I receive error
{{[ERROR ->]p.data | moment:‘dd/MM HH:mm’}}


#8

@lsantaniello you can directly use date pipe, there is no moment pipe in ionic.


#9

Thanks guys

I create my custom Pipe

import { Pipe, PipeTransform } from '@angular/core';

import * as moment from 'moment';

@Pipe({
  name: 'moment',
})
export class MomentPipe implements PipeTransform {

  transform(date, format) {
    return moment(date).format(format);
  }
  
}

usage:

<p>{{m.data | moment:'HH:mm'}}</p>

Thanks for support


#10

@AaronSterling’s date pipes are a much better choice.