How can I get the total number of days fr a specific month and year using JQuery custom UI?
Is there any fnction in the JQuery datepicker custom ui where I can get the total 开发者_如何学Pythonnumber of days by passing on the month name and year?
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
.
var y = 2012;
var m = 2;
var x = new Date(y,m,1,-1).getDate();
alert(x);
Output: 29
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?
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.
精彩评论