How to get the date list between two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitansu
    New Member
    • Dec 2013
    • 3

    How to get the date list between two dates

    i have two dates like 2013-1-1 and 2013-1-10 i want to get the output like 2013-1-1,2013-1-2,2013-1-3,.....2013-1-10 how i l get this give me some solution please.
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    is that dates from 2013-1-1 to 2013-1-10 are came from an array or in the database?,

    Comment

    • amitansu
      New Member
      • Dec 2013
      • 3

      #3
      ya this is coming 4m database only.Noproblem if u solve this by giving this values also i can able to do further

      Comment

      • Exequiel
        Contributor
        • Jul 2012
        • 288

        #4
        i think you can try this one. . .
        Code:
        $datas = mysql_query("SELECT * FROM tbl_date order by thedate asc");//your table name
        		while($data = mysql_fetch_array($datas))
        		{
        			echo $data['thedate'].'<br>';
        		}

        Comment

        • Exequiel
          Contributor
          • Jul 2012
          • 288

          #5
          its not a javascript, its php with sql I used the order by to order the date ascending . . . using that order by, it will automatically sort your data by the field name thedate

          Comment

          • amitansu
            New Member
            • Dec 2013
            • 3

            #6
            ya i got ur point but i need to write the programme in a servlet.

            Comment

            • Exequiel
              Contributor
              • Jul 2012
              • 288

              #7
              what do you mean by servlet.?

              Comment

              • Sherin
                New Member
                • Jan 2020
                • 77

                #8
                you can use the JS code

                Code:
                Date.prototype.addDays = function(days) {
                    var dat = new Date(this.valueOf())
                    dat.setDate(dat.getDate() + days);
                    return dat;
                }
                function getDates(startDate, stopDate) {
                    var dateArray = new Array();
                    var currentDate = startDate;
                    while (currentDate <= stopDate) {
                        dateArray.push( new Date (currentDate) )
                        currentDate = currentDate.addDays(1);
                    }
                    return dateArray;
                }

                Comment

                • SioSio
                  Contributor
                  • Dec 2019
                  • 272

                  #9
                  The date subtraction answer is returned in milliseconds.
                  Divide it by 86400000 to get the number of days.

                  Code:
                  var from = '2020-10-01';
                  var to = '2020-10-10'
                  var from_day = new Date(from);
                  var to_day = new Date(to);
                  var term = (to_day - from_day) / 86400000;
                  var date_arry = [from];
                  for (var i = 0; i < term; i++) {
                  	var dt = new Date(from_day.setDate(from_day.getDate() + 1));
                  	str = dt.getFullYear()
                  		+ '-' + ('0' + (dt.getMonth() + 1)).slice(-2)
                  		+ '-' + ('0' + dt.getDate()).slice(-2);
                  	date_arry.push(str);
                  }

                  Comment

                  Working...