ronan_40060
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
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 ?
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
}
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 ?
ASKER
Thanks
can you suggest a better approach ?
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 ) );
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.
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'));
@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.
When user selects a month and date, form a date string (with a known year), then pass it to my function.
ASKER
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
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 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() );
}
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.
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));
>>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 ?
What you mean by last business day, last friday of the month ?
ASKER
@cmalakar
User entered month would be stored in
var month = getMonthFromUserInput();
and then I can pass that mnth to
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
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();
}
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));
>>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.
There is no automatic way of doing this.. you should hard code this in your code.
ASKER
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());
}
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
Check javascript console. Press Ctrl+Shift+J in Firefox
ASKER
var userEnteredDate ;
var userEnteredMonth ;
then
var effectiveAsOfDateYear = document.forms[0].effectiv eAsOfDateY ear.value;
var effectiveAsOfDateMonth = document.forms[0].effectiv eAsOfDateM onth.value ;
var effectiveAsOfDateDay = document.forms[0].effectiv eAsOfDateD ay.value;
userEnteredDate = effectiveAsOfDateDay;
userEnteredMonth = effectiveAsOfDateMonth;
function LastDayVal(effectiveAsOfDa teMonth,ef fectiveAsO fDateDay ){
alert ("effectiveAsOfDateMonth is " + effectiveAsOfDateMonth);
alert("effectiveAsOfDateDa y is " + effectiveAsOfDateDay);
var lastDayDate = new Date ((new Date(effectiveAsOfDateMont h,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 userEnteredMonth ;
then
var effectiveAsOfDateYear = document.forms[0].effectiv
var effectiveAsOfDateMonth = document.forms[0].effectiv
var effectiveAsOfDateDay = document.forms[0].effectiv
userEnteredDate = effectiveAsOfDateDay;
userEnteredMonth = effectiveAsOfDateMonth;
function LastDayVal(effectiveAsOfDa
alert ("effectiveAsOfDateMonth is " + effectiveAsOfDateMonth);
alert("effectiveAsOfDateDa
var lastDayDate = new Date ((new Date(effectiveAsOfDateMont
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
ASKER
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() );
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.