1

I'm working with IONIC2, Angular2 and Typescript. I have an Datetime working as follows:

page.html

<ion-datetime displayFormat="DD MMMM YYYY" pickerFormat="DD MMMM YYYY" [(ngModel)]="date"></ion-datetime>

page.ts

  date: string = new Date().toISOString();

The ion-datetime field shows time with an hour less, how can I display date on Datetime picker considering the timezone?

Samuel Fraga Mateos
  • 583
  • 1
  • 7
  • 21
  • Why are you giving a string to that component? Tried without that `.toISOString()` ? – slaesh Sep 21 '16 at 08:37
  • Yes @mxii, I've tried only with `date: Date = new Date();` but an error occurs: 'ORIGINAL EXCEPTION: TypeError: Cannot read property 'month' of null'. I think this happens because ion-datetime try to parse a string with a especific format (ISO) not a Date or another string like `.toString()`, `toUTCString()`, `toLocaleString()`. – Samuel Fraga Mateos Sep 21 '16 at 08:53
  • That `ISOString` will return the UTC time without timezone info in it.. I don't know how to tell Ionic to show it using the local-timezone.. – slaesh Sep 21 '16 at 09:11
  • 1
    @SamuelFragaMateos please take a look at [this answer](http://stackoverflow.com/questions/39581875/ionic-2-beta-11-initializing-datetime-component-to-account-for-local-timezone/39600378#39600378). Hope it helps :) – sebaferreras Sep 21 '16 at 09:24
  • 1
    @sebaferreras that works for me!! Thank you very much. – Samuel Fraga Mateos Sep 21 '16 at 10:22

2 Answers2

3

Reading this answer I solve my problem. Finally I use:

moment(new Date().toISOString()).locale('es').format();

Thanks to sebaferreras

Community
  • 1
  • 1
Samuel Fraga Mateos
  • 583
  • 1
  • 7
  • 21
3

the simplest is to remove timezone offset in milliseconds:

date = new Date();
myDate: String = new Date(this.date.getTime() - 
                 this.date.getTimezoneOffset()*60000).toISOString();
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Atif
  • 51
  • 6