katemihalikova / ion-datetime-picker

Date and/or time picker for awesome Ionic framework
MIT License
169 stars 101 forks source link

Locale issue when selecting only time #58

Closed jason4zhu closed 7 years ago

jason4zhu commented 7 years ago

My project is built upon ionic, and the code in html is as follows:

<button class="button button-small button-light" time ion-datetime-picker ng-model="s"> text:{{s}} </button>

When I select a time via datetime-picker, it outputs "text:"1899-12-31T14:13:00.000Z"", which should be "2016-08-09T15:13.00.000Z". The date picker has the same problem. Could anyone help me out with this sort-of locale problem? Thanks.

katemihalikova commented 7 years ago

Hi Jason, I've created a codepen with your setup. As the model is always a Date object, you should not handle it as a string.

If you select 15:13 in picker, your model s is Date instance, which, when converted to string as in your example, outputs "1899-12-31T14:13:00.000Z" (your selected time in ISO8601 format). If you want the time in the local timezone, extract hours and minutes from it yourself or using moment library or similar.

var hours = $scope.s.getHours();
var minutes = $scope.s.getMinutes();
// or
var time = moment($scope.s).format("H:mm");
jason4zhu commented 7 years ago

Great! Thanks it works :P. But one more thing I'm still confused. If s is Date instance standing for 15:13, Why will it output 14:13 when printing to console?

katemihalikova commented 7 years ago

I don't know which browser are you using, but my Chrome does this in console:

» new Date()
« Tue Aug 09 2016 16:06:20 GMT+0200
» new Date().toISOString()
« "2016-08-09T14:06:20.762Z"

The first output shows time in local timezone, the second in UTC (2 hours difference here in Central Europe).