longbill / jquery-date-range-picker

A jQuery plugin that allows user to select a date range
MIT License
1.12k stars 579 forks source link

Wrong time calculation when start or end of range is in DST change day #485

Open xplwowi opened 4 years ago

xplwowi commented 4 years ago

Algorithm used in changeTime() and setRange() works incorrectly when start or end date falls on DST change day.

Currently both functions set date/time to beginning of the day then add to it selected hours and minutes, this way:

opt[name] = parseInt(
    moment(parseInt(date))
    .startOf('day')
    .add(moment(opt[name + 'Time']).format('HH'), 'h')
    .add(moment(opt[name + 'Time']).format('mm'), 'm')
    .valueOf()
);

For example, for year 2020 when selected time range is 00:00 – 23:59:

Problem can be solved by setting hours and minutes directly instead of adding them to the beginning of the day.

opt[name] = parseInt(
    moment(parseInt(date))
    .startOf('day')
    .hour(moment(opt[name + 'Time']).format('H'))
    .minute(moment(opt[name + 'Time']).format('m'))
    .valueOf()
);