2011-08-08 181 views
0

我有一个计算用户验证一个月的最后日期

var effectiveAsOfDateYear = document.forms[0].effectiveAsOfDateYear.value; 
var effectiveAsOfDateMonth = document.forms[0].effectiveAsOfDateMonth.value;   
var effectiveAsOfDateDay = document.forms[0].effectiveAsOfDateDay.value;     

userEnteredDate = effectiveAsOfDateDay; 

userEnteredMonth = effectiveAsOfDateMonth; 

// **Then using if condition** 
if (!isLastDayOfMonth(userEnteredDate, userEnteredMonth)) { 
alert("Inside isLastDayOfMonth of continueUploadReportAction "); 
// Do something   
} 
------------------------------------------------------------------ 
// The function is defined as below **strong text**   
function isLastDayOfMonth(date, month) { 
alert("Inside isLastDayOfMonth, the date is " + date); 
alert("Inside isLastDayOfMonth, the month is " + month); 
return (date.toString() == new Date(date.getFullYear(), month, 0, 0, 0, 0, 0).toString()); 
} 

进入一个月的最后日期。然而在运行时,我选择了每月7日为24, 两种功能传递给isLastDayOfMonth函数的实际值是 alert("Inside isLastDayOfMonth, the date is " + date);是6 和alert("Inside isLastDayOfMonth, the month is " + month);是24 并且返回似乎是不正确的。

请提出一个更好的办法..

回答

3

如果你有一个JavaScript的“日期”对象,你可以检查,看它是否是一个月这样的最后一天:

function isLastDayOfMonth(d) { 
    // create a new date that is the next day at the same time 
    var nd = new Date(d.getTime()); 
    nd.setDate(d.getDate() + 1); 

    // Check if the new date is in the same month as the passed in date. If the passed in date 
    // is the last day of the month, the new date will be "pushed" into the next month. 
    return nd.getMonth() === d.getMonth(); 
} 
相关问题