0

I want to get all dates in between 2 dates. So here I have mentioned statdate is date and end date is weekdate. In between 2 dates I want all dates.

Actully I am getting all dates But Not proper Format ,what i want in this format DD/MM/YY.

Now I am Getting in default Format (Sat Jun 09 2007 17:46:21)

$(document).ready(function () {
    $("#day").click(function () {
        startJsonSession();
        return false;
    });
    function startJsonSession() {
        var inputdate = $('#inputdate').val();
        //alert("Input Date!!!" + inputdate );
        var d = new Date(inputdate);
        var nowMS = d.getTime(); // get # milliseconds for today
        //alert(nowMS);
        var week = 1000 * 60 * 60 * 24 * 7; // milliseconds in one week
        //alert(week);
        var oneWeekFromNow = new Date(nowMS + week);
        //alert("oneWeekFromNow!!!" + oneWeekFromNow);
        var fromdate = d.getDate();
        var month = d.getMonth() + 1;
        var year = d.getFullYear();
        if (fromdate < 10) {
            fromdate = "0" + fromdate;
        }
        if (month < 10) {
            month = "0" + month;
        }
        //var date = fromdate + "/" + month + "/" + year;
        var date = year + "/" + month + "/" + fromdate;
        alert("InputDate!!!!" + date);

        //var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
        var weekdate = year + "/" + month + "/" + oneWeekFromNow.getDate();
        alert("weekdate!!!" + weekdate);
        var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));
        var tomorrowdate = tomorrow.getDate();
        var month1 = tomorrow.getMonth() + 1;
        var year1 = tomorrow.getFullYear();
        if (tomorrowdate < 10) {
            tomorrowdate = "0" + tomorrowdate;
        }
        if (month1 < 10) {
            month1 = "0" + month1;
        }
        //var nextday = tomorrowdate + "/" + month1 + "/" + year1;
        var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
        alert("tomorrow!!!!" + nextday);
        var d1 = new Date(date);
        alert("D1!!!!!" + d1.);
        var d2 = new Date(weekdate);
        var aDates = [];
        do {
            aDates.push(d1.toString());
            d1.setDate(d1.getDate() + 1);
        }
        while (d1 <= d2);
        alert("Dates!!!" + aDates);
        //alert(aDates.join("\n")); 
    }
});
1
  • new Date(inputdate) is reliant on Date.parse, which is implementation dependent, even for the format specified in ES5. Adding one week is a simple as date.setDate(date.getDate() + 7), similar for adding one day to get tomorrow. Commented Aug 8, 2014 at 8:42

3 Answers 3

1

You can do it in this way

$("#getDate").click(function () {
    var start = $("#startdate").datepicker("getDate"),
        end = $("#enddate").datepicker("getDate");

    currentDate = new Date(start),
    between = [];
    while (currentDate < end) {
        between.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);

    }

    for (var i = 0; i < between.length; i++) {
        var date = $.datepicker.formatDate('dd/mm/yy', new Date(between[i]));
        between[i] = date;
    }
    console.log(between)
})

Here 'between' is the array which contains all your required Date SEE DEMO HERE

Sign up to request clarification or add additional context in comments.

Comments

0
  alert("Dates!!!" + aDates.getDate()+"/"+ (aDates.getMonth()+1)+"/"+ aDates.getFullYear());

5 Comments

Code–only answers are not liked. Explain why the OP's code doesn't work and why yours does.
Because you call simple alert Date() to string it will return default format
if wrote like this what u Mention ,,Uncaught TypeError: Object [object Array] has no method 'getDate' is showing....@tuan hunyh
I didn't get u....@ RobG,if i did like this only date will show not month and year.
Thanks Roshan..Itz Working For Me..:)
0

You seem to want to get a array of date strings in d/m/y format given an input string in the same format. The following functions will do that.

// Parse a string in dmy format
// return a date object, NaN or undefined
function parseDMY(s) {
  var b = s.match(/\d+/g);
  if (b) {
    return new Date(b[2], --b[1], b[0]);
  }
}

// Given a date object, return a string in dd/mm/yyyy format
function formatDMY(date) {
  function z(n){return (n<10? '0' : '') + n;}
  return z(date.getDate()) + '/' + z(date.getMonth() + 1) + '/' + date.getFullYear();
}

function getWeekDates(s) {
  var d = parseDMY(s);
  var dates = [];
  if (d) {
    for (var i=0; i<7; i++) {
      dates.push(formatDMY(d));
      d.setDate(d.getDate() + 1);
    }
    return dates;
  }
}

console.log(getWeekDates('7/7/2014').join());
// 07/07/2014,08/07/2014,09/07/2014,10/07/2014,11/07/2014,12/07/2014,13/07/2014

Note that adding 1 day to a date is preferred over adding milliseconds as it allows the Date object to take account of daylight saving changes that might be involved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.