jquery datepicker getTime not UTC

javascript, jquery, jquery-ui, jquery-ui-datepicker, utc

Solution

It looks like the datepicker doesn’t return UTC dates, but local ones (which is actually the default in Javascript).

To convert your constructed dates to local time:

$("select.startdates").find("option").each( function() {
  var d = Date.UTC.apply(Date, this.value.split(",").map(Number));
  d = d + new Date(d).getTimezoneOffset() * 60000; // convert UTC to local
  startDates.push(d);
});

Normally, I’d use the `new Date(year, month, day)` constructor instead of the `Date.UTC` function, but you can’t use `apply` with the `Date` constructor.

If you’d rather leave your startDates array in UTC, then you need to convert the datepicker’s dates to UTC:

function showEventDates(date) {
  date = date - new Date(date).getTimezoneOffset() * 60000; // convert local to UTC
  // for ...
}

NB: choose one or the other of these methods, not both, or you’ll end up with the same problem... :-)

Problem

I have the following code which works fine until the datepicker reaches BST. ``` var i; function showEventDates(date) { for (i = 0; i < startDates.length; i++) { if (date.getTime() == startDates[i]) { return [true, 'eventDay']; } } return [false, '']; } var startDates = new Array(); $("select.startdates").find("option").each( function() { startDates.push(Date.UTC.apply(Date, this.value.split(",").map(Number))); }); $('#mydate').datepicker({ beforeShowDay: showEventDates }); ``` During BST the line `if (date.getTime() == startDates[i]) {` returns false because there's an hour difference. Any ideas how I can get these to match? I think it's the datepicker time that's not UTC. EDIT: An example of an option from select.startdates is ``` <option value="2013, 2, 1">01/03/2013</option> ```

Original source