1

Here I'm trying in jQuery to print the number of the dates and Full weekday on particular day but it gives me the output shows like below:-

Output

[Fri Jun 01 2018 00:00:00 GMT+0530 (IST),
 Sat Jun 02 2018 00:00:00 GMT+0530 (IST),
 ......,
 ......, //further till end date
]

The code I'm using is :-

 var start = $("#txtFromDate").datepicker("getDate"),
  end = $("#txtToDate").datepicker("getDate"),
  currentDate = new Date(start.getTime()),
  between = []
;

while (currentDate <= end) {
    between.push(new Date(currentDate));
    currentDate.setDate(currentDate.getDate() + 1);
}
    console.log(between);

Edited code:-

var start = $("#txtFromDate").val($.datepicker.formatDate('M dd yyyy', new Date())),
end = $("#txtToDate").val($.datepicker.formatDate('M dd yyyy', new Date())),
currentDate = new Date(start.getTime()),
between = []
;

while (currentDate <= end) {
    between.push(new Date(currentDate));
    currentDate.setDate(currentDate.getDate() + 1);
}
console.log(between);

I tried this code too but not getting any useful output but it gives an error:-

Uncaught TypeError: start.getTime is not a function

at HTMLButtonElement. (setschedule:227)

at HTMLButtonElement.dispatch (jquery-1.12.4.js:5226)

at HTMLButtonElement.elemData.handle (jquery-1.12.4.js:4878)

Output I need is:-

 [Friday, 06-01-2018,
  Saturday, 06-02-2018,
  ......,
  ......, //further till end date
 ]

How can I will get my output Any help can be appreciate. Thank you.

Community
  • 1
  • 1
vikas kumar
  • 117
  • 13
  • Look at date formatting. https://stackoverflow.com/questions/5250244/jquery-date-formatting – zanerock May 22 '18 at 12:53
  • @zanerock i tried this code i edit my question can you help me? – vikas kumar May 22 '18 at 13:05
  • The [`formatDate`](http://api.jqueryui.com/datepicker/#utility-formatDate) returns a string, not a date. So you probably want to format the date when you do the push, and then have an array of formated strings representing dates: `between.push($.datepicker.formatDate('M dd yyyy', currentDate));` – zanerock May 22 '18 at 17:32
  • @zanerock what about days? – vikas kumar May 23 '18 at 03:06
  • Are you asking how to print the names of the days? Check out the `formatDate` docs linked in previous comment. – zanerock May 23 '18 at 13:13

1 Answers1

1

After you build the array, use .pop() to remove the last element, and use .shift() to remove the first element. That should leave you with only the dates between.

Luke G.
  • 587
  • 1
  • 4
  • 13