Try to search your question here, if you can't find : Ask Any Question Now ?

How to add dates between two dates in an array using Javascript or PHP?

HomeCategory: stackoverflowHow to add dates between two dates in an array using Javascript or PHP?
Avatarpushpa asked 5 months ago

I’m setting up a Graph using CanvasJS. Everything working fine. But in dates array, if there are no dates between from date object date: 23-7-2019 09:00 AM, value: 20 to date object date: 13-10-2019 11 AM, value: 25 the graph showing a straight line between these to dates. So I want to add dates for each day between those dates and want to set the value as null. so that graph will show the empty gaps instead of straight line.

So earlier I added value as null if the values or negative values. I have added the code below.

var mydatapoints4 = [];
  for(var k=0;  k< list_values.length; k++){
    var tk= list_values[k].x;
    var uk = list_values[k].y;
    if(uk > 0){
       mydatapoints4.push({x: new Date(tk), y: uk});
    }
    else{
    mydatapoints4.push({x: new Date(tk), y: null});
    }
  };

So My datapoints 4 array is like this

0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Tue Apr 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
3: {x: Wed Apr 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}

I want the output of the array like this.

0: {x: Fri Apr 06 2018 22:30:00 GMT-0500 (Central Daylight Time), y: 93.9}
1: {x: Sat Apr 07 2018 09:00:00 GMT-0500 (Central Daylight Time), y: 69.3}
2: {x: Sun Apr 08 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
3: {x: Mon Apr 09 2018 09:00:00 GMT-0500 (Central Daylight Time), y: null}
4: {x: Tue Jun 10 2018 10:00:00 GMT-0500 (Central Daylight Time), y: 71.2}
5: {x: Wed Jun 11 2018 11:00:00 GMT-0500 (Central Daylight Time), y: 67.2}

I hope my question is clear to you, and may I know how can I do that? if you can help me it’s a huge help to me.

Thanks for the support.

1 Answers
Best Answer
AvatarMikhail answered 5 months ago
Your Answer

12 + 0 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com