Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

asked on

How to validate user input date is the last day of the month using javascript?

friends

I have a JSP page where in a user selects a Month and a date and then I have to validate the selected date is the last date for that month  and then set a flag true
So far I have this

var month = getMonthFromUserInput();
var curdate= getDateFromUserInput();
function getlastDay(month ){
var lastDate = new Date((new Date( month , 1)) -1 );
if (lastDate == curdate){
return true ; 
}
return false 
}

Open in new window


Please suggest a better approach .
Also returning a true / false is better or setting a flag to a some value is better  inside the function ?
Avatar of Proculopsis
Proculopsis


For brevity you can return ( thisValue == thatValue ); for a boolean result.

I think your code is based on 1970 where it should be relative to the current year to cater for the 29th of February in leap years.

Avatar of ronan_40060

ASKER

Thanks
can you suggest a better approach ?

//Try something like this:

function isLastDayOfMonth( date, month ) {
  return ( date.toString() == new Date( date.getFullYear(), month, 0, 0, 0, 0, 0 ).toString() );
}

var test = new Date( "2012/2/28" );

alert( isLastDayOfMonth( test, 2 ) );

test = new Date( "2012/2/29" );

alert( isLastDayOfMonth( test, 2 ) );
You can do it like this...

Just add one day to the given date, then if the date goes to a different month, then it is last day.
function isLastDate(date){
var givenDate = new Date(date);
var nextDayDate = new Date(date);
nextDayDate.setDate(givenDate.getDate() + 1);
return !(nextDayDate.getMonth() == givenDate.getMonth());
}

alert(isLastDate('2008/2/29'));

Open in new window


@cmalakar - No, the solution requires last day of a given month, not just any month.
@Proculopsis, still that should work for a given month too..

When user selects a month and date, form a date string (with a known year), then pass it to my function.
Thanks a lot
I just need to pass the Month to the function and then it will calculate the last day of that month and will compare it with what the user has input .
1)  so one solution is instead of passing both a user selected date and a month , only a month can be passed and then the last day for that month can be calculated and compared to the user entered date
@Proculopsis: as per sur solution

function isLastDayOfMonth( date, month ) {
  return ( date.toString() == new Date( date.getFullYear(), month, 0, 0, 0, 0, 0 ).toString() );
}

Open in new window

i just need to pass the month so how will ur function work ?
2) solution is as suggested by cmalakar to check whether the date entered by the user is the last day
@cmalakar
How will your function behave if i have to calculate the last  business day of the month ?

Thanks a lot for your time and suggestions

>>I just need to pass the Month to the function and then it will calculate the last day of that month

Then use the below function.

give month values as jan - 1, feb - 2, march - 3, etc.
function getLastDayOfMonth(month){
var firstDayDate = new Date(new Date().getFullYear(), (month), 0, 0, 0, 0, 0);
return firstDayDate.getDate();
}
alert(getLastDayOfMonth(4));

Open in new window

>>How will your function behave if i have to calculate the last  business day of the month

What you mean by last business day, last friday of the month ?
@cmalakar
User entered month would be stored in
var month = getMonthFromUserInput();

and then I can pass that mnth to
function getLastDayOfMonth(month){
var firstDayDate = new Date(new Date().getFullYear(), (month), 0, 0, 0, 0, 0);
return firstDayDate.getDate();
}

Open in new window


I will have to assign values to each month and then pass the values to the function instead .

so what you have suggested seems a better approach as I just have to check for that date only :)
butu what if the last business day is the last friday of the month for that country e.g US
and also I need to know the US holidays also
If you want to get the date of last Friday of the given month, then use below.
function getLastFridayDateOfMonth(month){
var firstDayDate = new Date(new Date().getFullYear(), (month), 0, 0, 0, 0, 0);
var lastDay = firstDayDate.getDay();
var daysToReduce = 0;
if(lastDay < 5){
 daysToReduce = lastDay + 2;
}
if(lastDay == 6){
  daysToReduce = 1;
}
firstDayDate.setDate(firstDayDate.getDate() - daysToReduce);
return firstDayDate.getDate(); 
}
alert(getLastFridayDateOfMonth(12));

Open in new window

>>also I need to know the US holidays also

There is no automatic way of doing this.. you should hard code this in your code.
function LastDayVal(effectiveAsOfDateDay){
	alert("Inside LastDayVal");
	var inputDate = effectiveAsOfDateDay;
	alert("Inputdate is" + inputDate); 
	var nextDateDay = effectiveAsOfDateDay;
	alart("nextDateDay is " + nextDateDay)
	nextDateDay.setDate(inputDate.getDate() +1 );
	alert("NextDateDay is " + nextDateDay);
	return !(nextDayDate.getMonth() == inputDate.getMonth());
	  
}

Open in new window

while running the code
it doesnt print alart("nextDateDay is " + nextDateDay)
There may be some error in javascript code.

Check javascript console. Press Ctrl+Shift+J in Firefox
var userEnteredDate ;
var userEnteredMonth ;
then
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;
function LastDayVal(effectiveAsOfDateMonth,effectiveAsOfDateDay ){
        alert ("effectiveAsOfDateMonth is " + effectiveAsOfDateMonth);
       alert("effectiveAsOfDateDay is " + effectiveAsOfDateDay);      
      var lastDayDate = new Date ((new Date(effectiveAsOfDateMonth,1)) -1 ) ;
      alert ("lastDay is " + lastDayDate);
      return (lastDayDate == effectiveAsOfDateDay )
 }
While running above code
im passing the effectiveAsOfDateMonth as 7,effectiveAsOfDateDay as 24
while im printing inside the LastDayVal function
the value of effectiveAsOfDateMonth  comes as 6
and the value effectiveAsOfDateDay  comes as 24
and the value of lastDayDate  comes as Wed Jan 31 23:59:59 UTC +530 1906
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() );  
           } 

Open in new window

However while runtime the when I select the month as 7 and date as 24
then actual values passed to the isLastDayOfMonth function are alert("Inside isLastDayOfMonth ,
the date is " + date ); is 6 and alert("Inside isLastDayOfMonth , the month is " + month ); is 24 and return never seems to be correct
ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial