0
  1. I am trying to split day from full-date and calculating the range between them and inserting them into a array to add that to a database.
   <script>
    function()
    {

      var sd = "26-04-2020";
      var a = sd.split("-");

      var ed = "28-04-2020";
      var b = ed.split("-");

      var p1= a[1];
      var p2= a[2];
      var p3= a[3];

      var q1= b[1];
      var q2= b[2];
      var q3= b[3];

      var datearry = [];

      if( p1<q1 && p2=q2 && p3=q3)
        {
           for (i=p1; i<q1; i++)
           {
             datearry = ("i"+"p2"+"p3");
           }
        }
        else if( p1<q1 && p2<q2 && p3=q3)
       {
         for (i=p1; i<q1; i++)
         {
          for (j=p2; j<q2; j++)
          {
            datearry = ("i"+"j"+"p3");
          }
        }
       }
       else if( p1<q1 && p2<q2 && p3<q3)
        {
          for (i=p1; i<q1; i++)
          {
            for (j=p2; j<q2; j++)
            {
              for (k=p3; k<q3; k++)
              {
                datearry = ("i"+"j"+"k");
              }
            }
          }
        }
     alert(datearry);
  }
  </Script>
  1. I did not get the expected result, anyone please give some suggestions or even code that works thankyou.
chethan
  • 33
  • 4
  • 1
    What is "expected result"? Are you expecting to get a date for each day between the start and end date? – RobG Apr 24 '20 at 22:21
  • Your code is also missing a function name. ;-) – RobG Apr 24 '20 at 22:27
  • Convert start/end to actual dates, increase days by 1 until you get to the end date. Comparing `p1p2` – freedomn-m Apr 24 '20 at 22:50
  • Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – freedomn-m Apr 24 '20 at 23:02

1 Answers1

0

Your code has a number of issues:

 function()
    {

Is a syntax error, function declarations require a function name.

 datearry = ("i"+"p2"+"p3");

datearray is initialised as an array, but here a string literal is assigned (literally "ip2p3"). You probably meant:

datearray.push(String(i) + p3 + p3);

which will add a string like "26042020" the first time, however as the algorithm progresses and the strings are turned into numbers, you'll start to get values like "152020" for 1 March 2020. So you need some padding, and probably a delimiter, to get "01-05-2020" and match the input format.

Lastly, the code doesn't seem to correctly accommodate transitions over month and year boundaries. Fixing that really isn't useful as it's much more complex than it needs to be or I'm prepared to bother with given there are much simpler alternatives. :-)

If you're looking for an array of timestamps between two dates, one strategy is to convert the timestamps to dates, then loop over the required range storing the required output timestamps in an array.

Existing questions deal with how to parse a date string and how to format a date. You can put those together to create a function something like the following:

// startDate, endDate in format dd-mm-yyyy
// Return array of dates inclusive of start and end in same format
function getDateRange(startDate, endDate) {
  // Parse dd-mm-yyyy
  let qParse = s => {
    let [d, m, y] = s.split(/\D/);
    return new Date(y, m-1, d);
  };
  // Format date as dd-mm-yyyy
  let qFormat = d => {
    let z = n => (n<10? '0' : '') + n;
    return z(d.getDate())+'-'+z(d.getMonth()+1)+'-'+d.getFullYear();
  }
  // Setup for loop
  let start  = qParse(startDate);
  let end    = qParse(endDate);
  let result = [];
  // Loop from start to end, incrementing
  // the start date and writing to result array
  do {
    result.push(qFormat(start));
    start.setDate(start.getDate() + 1);
  } while (start <= end)
  
  return result;
}

console.log(getDateRange('26-02-2020','03-03-2020'));
// Different delimiters in input
console.log(getDateRange('28.12.2020','03/01/2021'));

You should also validate the input. The parse and format functions are closed in the function as they're only designed to work with the specific format of the OP (i.e. they aren't general parsing or formatting functions).

RobG
  • 124,520
  • 28
  • 153
  • 188