0

I have probably very simple question. I downloaded the http://jqplot.com javascript library and it provides the following example:

loc.push([[1340877779000, 2, 'start'] , [1340877869000, 2, 'end' ]]);

I need to substitute 1340877779000 with my variables that specify the time. I have:

var date_start = "2012-08-12 11:15";
var date_end = "2012-08-12 12:00";

How can I format 'date_start' and 'date_end' variables in order to use them inside loc.push()?

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Gusgus
  • 353
  • 2
  • 6
  • 22

4 Answers4

3

Just try:

var date_start =  +new Date('2012-08-12 11:15');

Update

However it doesn't seem to work in firefox and IE. For firefox this should work (webkit can also consume it):

+new Date('2012-08-12 11:15'.replace(/-/g, '/'));

I'm not sure about IE, don't have any to test.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • What is the `+` on the `new` do? – Jared Farrish Aug 12 '12 at 16:45
  • The same as `new Date('2012-08-12 11:15').getTime()`. This is type casting so the results converts to integer. – dfsq Aug 12 '12 at 16:46
  • Do you know of any documentation on that? I've never encountered that before, would like to read about it. – Jared Farrish Aug 12 '12 at 16:47
  • This has same problem as `Date.parse` Does not work in firefox or ie. @JaredFarrish Docs https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/valueOf – Esailija Aug 12 '12 at 16:48
  • @Esailija - Where does that explain the `+`? Or am I missing something? – Jared Farrish Aug 12 '12 at 16:49
  • 1
    Read here http://stackoverflow.com/questions/9430357/please-explain-why-and-how-new-date-works-as-workaround-for-date-now-in – dfsq Aug 12 '12 at 16:49
  • @JaredFarrish Yes, `+{valueOf: function(){alert("hello"); }}`. Trying to convert an object to a number triggers its `.valueOf` method. – Esailija Aug 12 '12 at 16:49
  • @Esailija - So it's the [unary operator](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Arithmetic_Operators#.2B_%28Unary_Plus%29)? – Jared Farrish Aug 12 '12 at 16:50
  • @JaredFarrish the operator isn't relevant. You could use `1* new Date` or `new Date - 0` or so on. All trigger the conversion to a number. – Esailija Aug 12 '12 at 16:51
  • @dfsq: Nice! I didn't know that! Very usefull. – davidbuzatto Aug 12 '12 at 16:51
  • This answer is still very wrong :) The result is `NaN` in firefox and IE. – Esailija Aug 12 '12 at 16:55
  • One strange thing that you will get `NaN` only if the `hour:minute` is specified. Without it, the parse works correctly. With the `replace` it works properly in Firefox. – davidbuzatto Aug 12 '12 at 17:00
0

Do you mean, how to place the variables inside the array?

loc.push([[date_start, 2, 'start'] , [date_end, 2, 'end' ]]);
Dan
  • 526
  • 9
  • 28
0

Use Date.parse

Date.parse("2012-08-12 11:15")
// returns 1344784500000

So it would follow that you should use

var date_start = Date.parse("2012-08-12 11:15");
var date_end = Date.parse("2012-08-12 12:00");
loc.push([[date_start, 2, 'start'] , [date_end, 2, 'end' ]]);
Dylan
  • 13,645
  • 3
  • 40
  • 67
0

Try this:

var date_start = new Date("2012-08-12 11:15").getTime();
var date_end = new Date("2012-08-12 12:00").getTime();
loc.push([[date_start, 2, 'start'] , [date_end, 2, 'end' ]]);
bpatel
  • 381
  • 1
  • 4