0

I'm making a JSON call to COSM (now Xively) so it will return data that I can plot with Highchart's highstock chart. See: jsfiddle.net/T7D5U/2/

Currently the start and end date are hard coded like this:

 $.getJSON('http://api.xively.com/v2/feeds/4038/datastreams/9.json?start=2013-05-01T00:00:00Z&end=2013-05-19T23:00:00Z&interval=3600?key=dNSiSvXZtR6QBUqbzll4CCgnngGSAKxIQVFSeXBneGpqWT0g', function(data) {

I want the start and end dates to be dynamic. I want the end date and time to be now. If now was May 19, 2013 2:30 PM, it would be formatted like this:

end=2013-05-19T14:30:00Z

And I'd like the start time to be now minus 10 days, this can be rounded to the day. So the start time would look like this:

start=2013-05-09T00:00:00Z 

BTW, I'm not familiar with JavaScript (just C).

Also, when I try an put a jsfiddle link in stackoverflow post, I get an error that says "Links to jsfiddle.net must be accompanied by code." I'm confused by this; I don't know what I'm supposed to do.

errordeveloper
  • 6,716
  • 6
  • 41
  • 54
  • RE: "Links to jsfiddle.net must be accompanied by code" - they probably mean that you should add questionable parts of the code into the post and then link to jsfiddle.net for the full version of the code... – errordeveloper May 20 '13 at 09:58

1 Answers1

0

I will do it that way :

// Set end to current date and time on client
var end = new Date();

// Copy end date and assign to start
var start = new Date(+end);

// Set date of start to 10 days ago
start.setDate(start.getDate() - 10);

alert(start.toISOString());
RobG
  • 142,382
  • 31
  • 172
  • 209
Cyril
  • 537
  • 4
  • 11
  • That's pretty slick, but something isn't quite right. When I do an alert(end.toISOString()); instead of start, it returns the time offset by 10 days. So I'm getting the end time to be the same as the start time which is May 10. See http://jsfiddle.net/scott216/qF2GD/ – Scott Goldthwaite May 20 '13 at 01:41
  • @ScottGoldthwaite—that's because `end`, `currentTime` and `start` all reference the same Date object. To copy the object use `start = new Date(+end);`, then set the new date for one or the other as appropriate. – RobG May 20 '13 at 02:02
  • I got it working. I did this:
    var currentTime = new Date();
    var end = currentTime;
    var start = new Date(currentTime.getTime() - 10 * 24 * 60 * 60 * 1000);
    – Scott Goldthwaite May 20 '13 at 02:27
  • Note that *toISOString* is an ES5 method so you might need to test for it and provide an alternative if it's missing. – RobG May 20 '13 at 02:36
  • @ScottGoldthwaite–don't do that, you will get some errors around daylight saving changeover when days aren't 24 hrs. Better to do the date thing. – RobG May 20 '13 at 02:38