user1891513
user1891513

Reputation: 1

validate date should be last date of month in javascript

My requirement is to create a month which contains two date fields from date and to date

from date should be starting date of month and end date should be ending date of month..if from date is not starting date it should show message please select starting date of month same for to date also

i tried with this code

function getDaysInMonth(aDate){

var m = new Number(aDate.getMonth());
var y = new Number(aDate.getYear());

var tmpDate = new Date(y, m, 28);
var checkMonth = tmpDate.getMonth();
var lastDay = 27;

while(lastDay <= 31){
    temp = tmpDate.setDate(lastDay + 1);
    if(checkMonth != tmpDate.getMonth())
        break;
    lastDay++
}
return lastDay;

}

its not working pls help me

Upvotes: 0

Views: 356

Answers (2)

Brian Garson
Brian Garson

Reputation: 1160

why not just use the new Date() function in javascript?

new Date(year, month, day) months go from 0-11 using a day of 0 will result in the previous months last day.

//january 1st, 2012 
var d = new Date(2012, 0, 1);
alert(d)

//last day of january 2012 (month 1, day 0)
var d2 = new Date(2012, 1,0);
alert(d2);

Upvotes: 0

palaѕн
palaѕн

Reputation: 73926

Try this:

function daysInMonth(iMonth, iYear)
{
    return new Date(iYear, iMonth, 0).getDate();
}

Upvotes: 1

Related Questions