ng2-ui / datetime-picker

Angular2 DateTime Picker
https://ng2-ui.github.io/#/datetime-picker
MIT License
121 stars 62 forks source link

0 minutes in time picker #9

Closed Redigast13 closed 8 years ago

Redigast13 commented 8 years ago

If I click on input field then I see correct year, month, day, hour. But minutes are always equal 0. Here is your example with this error: https://ng2-ui.github.io/#/datetime-picker There is an input field with 2015-01-01 23:59 If you click on it, then you will see 2015-01-01 23:00

allenhwkim commented 8 years ago

Thanks for reporting this. That's strange though. I don't see that happen for me.

image

Do you have any specific env. e.g., Windows, Safari, Linux, etc?

Redigast13 commented 8 years ago

Windows 7. The problem is in initDateTime function in datetime-picker.component.ts.

console.log('initDateTime', date, typeof date);
if (typeof date === 'string') {
    date = this.dateTime.fromString(<string>date);
    console.log('after fromString', date, typeof date); // I added this string
}

Input time is: 2016-08-12 02:44 date-format attribute: dd.MM.y HH:mm Output in console:

initDateTime 2016-08-12 02:44 string
dateStr 2016-08-12 02:44+-5:00
after fromString Fri Aug 12 2016 02:00:05 GMT+0500 (RTZ 4 (зима)) object
allenhwkim commented 8 years ago

Thanks for pin-pointing the exact problem.

    dateStr = this.removeTimezone(dateStr);
    dateStr = dateStr + this.addDSTOffset(dateStr);

    let tmp = dateStr.split(/[-:\ T]/); // split by dash, colon or space
    console.log('dateStr', dateStr);

This works well in minus time zone(-05:00) but buggy in plus time zone(+0500).

dateStr = '2016-08-12 02:44+-5:00';
dateStr.split(/[-:\ T]/); 
["2016", "08", "12", "02", "44+", "5", "00"]

That "44+" caused the issue. +"44+" results in NaN

I found out that the following change will make a fix.

addDSTOffset(dateStr): string {
    let date = new Date(dateStr);
    let jan = new Date(date.getFullYear(), 0, 1);
    let jul = new Date(date.getFullYear(), 6, 1);
    let stdTimezoneOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
    let isDST = date.getTimezoneOffset() < stdTimezoneOffset;
    let offset = isDST ? stdTimezoneOffset - 60 : stdTimezoneOffset;
    let diff = offset >=0 ? '-' : '+';
    offset = Math.abs(offset);    //<-------- this will make a fix
    return diff +
        ('0'+ (offset / 60)).slice(-2) + ':' +     
        ('0'+ (offset % 60)).slice(-2);
  };

I will make this change and will be pushed with RC5 upgrade. Meanwhile, feel free to make adjustment on your own.

allenhwkim commented 8 years ago

upgrade to RC5 and this error has been fixed