0

I have a select menu with options looking something like this...

<option value="2013, 2, 1">01/03/2013</option>

I want to create an array of the dates from this menu but as milliseconds

so I need something like:

myArray = [1368140400000,... etc]

Any ideas how I can get this? I've tried this so far but it doesn't work, returning NaN instead.

var startDates = new Array;
$("select.startdates").find("option").each( function() {
    startDates.push(new Date($(this).val()).getTime()) 
});
Tom
  • 12,776
  • 48
  • 145
  • 240
  • What timezone do you expect this date to be? – Bergi Feb 19 '13 at 14:18
  • GMT, that's what's caused the problem... I was holding the option value as milliseconds from the server, but this different from the a javascript date during BST – Tom Feb 19 '13 at 14:22
  • 1
    Can't you just have the dates in milliseconds in your `value` attributes? – bfavaretto Feb 19 '13 at 14:22
  • OK, so I've done that, but it appears that there's a problem further in my code where the datepicker date.getTime() is different to the javascript conversion of a date to milliseconds when it's BST – Tom Feb 19 '13 at 14:33

1 Answers1

1

2013, 2, 1 is no valid date that is recognized by Date.parse. This should work better:

var ms = Date.UTC.apply(Date, this.value.split(",").map(Number));

However, I think it should be easier to store the milliseconds themselves in the option value, so that you can easily use new Date(parseInt(this.value, 10))

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • OK, so based on this I've implemented your advice and uncovered another problem where the date in milliseconds from the array is different to the date in milliseconds from the jquery's datepicker when it is BST. It doesn't seem to compensate for the hour adjustment – Tom Feb 19 '13 at 14:41
  • Can't you configure the datepicker to show UTC instead of local datetimes? – Bergi Feb 19 '13 at 14:43