Thursday 3 July 2014

Get all the dates between two dates - Javascript 7

We can do this using Javascript's Date() object. You can use this following function to get between two dates. It will return array. Here we are using isDate() and isValidRange() functions to make sure is the given string is Date and end date is not greater then start date. If the given string is not date, then it will return "error occured!!!... Please Enter Valid Dates" in browser console and it will return empty array.

Parameters:

Start - Starting Date
end - Ending Date
Note: you should pass Date object as parameters.
If you are using jQuery Date picker, you can get selected date object like below,
var start = $("#from").datepicker("getDate"),
    end = $("#to").datepicker("getDate");
you can get selected date object using 'getDate' parameter in datepicker.

usage and Example :

If you are using datepicker, you can use this code,
var start = $("#from").datepicker("getDate"),
    end = $("#to").datepicker("getDate");

var between = getDates(start, end);

If you are going to use your own date string, you should make it as date object. Then only you can use in it. You can use this piece of code,
var start = new Date('2014-07-13'),
    end = new Date('2014-07-17');

var between = getDates(start, end);

Live Demo

Have any doubt, feel free to comment here!

7 comments:

  1. Hi Cj, tried this code, but i am struggling with displyang the dates in between.
    when i click the button, i does not display anything. please help

    ReplyDelete
  2. Hi Cj, tried this code, but i am struggling with displyang the dates in between.
    when i click the button, i does not display anything. please help

    ReplyDelete
    Replies
    1. Sorry for the late reply.. Any errors showed in browser console?

      Delete
  3. It is not working is it using jquery ?

    ReplyDelete
  4. i want to display just the date and day what modifications i should do in this.i dont want to display gmt 5:30 and all

    ReplyDelete