2

I'm using Ionic 3. I used ion-datetime component:

myDate: String = new Date().toISOString();

<ion-datetime displayFormat="HH:mm" pickerFormat="HH:mm" [(ngModel)]="myDate"></ion-datetime>

I want to have today's date that depends on the timezone of the user device. Unfortunately new Date().toISOString() always returns the GMT one. any advises how to this properly ?

Melchia
  • 22,578
  • 22
  • 103
  • 117

3 Answers3

5

If you use toLocalString() it gives you the following format MM/DD/YYYY, HH:MM:SS A

If you need the a ISO 8601 format consider using the below code:

mydate: String = (new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000)).toISOString().slice(0, -1);
Mario Shtika
  • 1,436
  • 19
  • 31
1

try using the toLocaleString() function of the Javascript Date Object.

 myDate: String = new Date().toLocaleString();

Hope this helps.

1
  1. Open console at root proyect and install moment: npm install moment --S.
  2. Import moment in component file: import moment from 'moment';.
  3. Set value of model variable: this.myDate = moment().format().

Check this post for more details: https://stackoverflow.com/a/47843362/7829826

Daniel Delgado
  • 4,813
  • 5
  • 40
  • 48