0

When I set a date with native JS tools, with an ISO date as the input, the date that I obtain is: Wed Apr 29 2020 02:00:00 GMT+0200, so the hour is 2 p.m.

When I set a date using jQuery UI datepicker with the code below, I end up with: Wed Apr 01 2020 00:00:00 GMT+0200 and Wed Apr 29 2020 00:00:00 GMT+0200, so the hour is 0 p.m.

I need to compare date to date_min and date_max, and this difference with the hours doesn't help.

Apparently the 2 p.m. from the first method is time zone dependant. What is the best way to make my dates easily comparable?

Thank you for your time!

<script>
var date = new Date('2020-04-29');

var date_min, date_max;
$(function() {
    $('#from').datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function() {
            date_min = $(this).datepicker('getDate');
            date_max = $('#to').datepicker('getDate');

            if (date && date_min && date_max)
                console.log(date, date_min, date_max, date.getTime(), date_min.getTime(), date_max.getTime());
        }
    });
    $('#to').datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function() {
            date_min = $('#from').datepicker('getDate');
            date_max = $(this).datepicker('getDate');

            if (date && date_min && date_max)
                console.log(date, date_min, date_max, date.getTime(), date_min.getTime(), date_max.getTime());
        }
    });
});
</script>

<div id="date">
    <label for="from">from</label>
    <input type="text" id="from" name="from">
    <label for="to">to</label>
    <input type="text" id="to" name="to">
</div>
scpsc
  • 1
  • 1
  • Use the Date methods to get the UTC version of the date, before getting the time. – Taplar Apr 29 '20 at 17:03
  • 2020-04-29 is parsed as UTC, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 29 '20 at 20:42
  • Welcome to Stack Overflow. As a reminder, Datepicker does not select a Time only a Date, hence why the time is set to 0 hours. – Twisty Apr 30 '20 at 14:18

1 Answers1

0

As pointed out by RobG, this answer solved my problem.

Thanks a lot!

scpsc
  • 1
  • 1