1

Is there any fnction in the JQuery datepicker custom ui where I can get the total number of days by passing on the month name and year?

Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102

4 Answers4

6

Just use regular Javascript. No need for jQuery. I'm not sure what the date picker returns but if you can get the year like '2012' and the month like '2':

var year = 2012;
var month = 2;
var days = Math.round(((new Date(year, month))-(new Date(year, month-1)))/86400000);

Since 2012 is a leap year, and 2 is February the above code days becomes 29.

Paul
  • 139,544
  • 27
  • 275
  • 264
3
var y = 2012;
var m = 2;
var x = new Date(y,m,1,-1).getDate();
alert(x);

Output: 29

Shrestha Ashesh
  • 1,769
  • 2
  • 13
  • 12
1
var y = 2012;
var m = 2;
var x = new Date(y,m,1,-1).getDate();
alert(x);

Explaining above answer... (couldn't comment there as my reputation is <50)

It simply constructs a new date object refering to date (remember month values are 0-11 so m=2 implies march) 1 March 2012 minus 1 hour which in turn will give 29 Feb 2014 23:00:00 and .getDate() doing rest of the work.

I think its better than other upvoted answers due to simplicity, less space & time complexity.

Ravi
  • 95
  • 1
  • 1
  • 8
1

You can pass the dates using the following methods, using a start and end date:

function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydifference(first, second) {
    return (second-first)/(1000*60*60*24)
}

//example
    alert(daydifference(parseDate($('#firstDate').val()), parseDate($('#endDate').val())));

More links you can have a look at :

Jquery get total days from 2 inputs and display

How do I get the number of days between two dates in JavaScript?

Community
  • 1
  • 1
Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72