<ion-item>
<ion-label>Date</ion-label>
<ion-datetime displayFormat="YYYY/M/D HH:mm " pickerFormat="YYYY MM D HH:mm +01:00" [(ngModel)]="myDate" min="{{minDisp}}" max="2030" placeholder="Choose Date"></ion-datetime>
</ion-item>
I would just like to have the same time I choose in the datetime picker when I convert it to a date object.
I’m always getting something like 2017-04-27T01:00:00Z and the “Z” always results in adding 2 hours when I convert this to a Date Object. which means I’d get 3:00 O’clock in this case.
myDate = new Date(2018,9,6,12,0);
myDate.setMinutes(myDate.getMinutes() - myDate.getTimezoneOffset());
// myDate.getTimezoneOffset() = 120 ... GMT-3 in my case
And before save the result into database, I made the inverse:
Why it solved my problem? Because getTimezoneOffset gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC).
And I will say more, the right way to work with dates and times is save it on back-end always with the “timestamp UTC” format. Consider convert this to end user always that it’s called and vice versa. You will never more have problems and your app will be prepared to internationalization.