CFDevHead
asked on
last day of the month
How do I test for the last day of the month?
here is what I am trying to do
function addEndDte(){
//alert(isDate(dte.value))
dte=BudgetCopy.BegDte.valu e;
if(isDate(dte)){
d="";
d=new Date(dte)
d.setMonth(d.getMonth()+4)
d.setDate(31)
document.BudgetCopy.EndDte .value=two d(d.getMon th()*1+1) + "/" + twod(d.getDate()) + "/" + d.getFullYear();
}else{
setTimeout("addEndDte()", 200);
}
}
if the month is Feb. and I try to add this d.setDate(31) its going to make the month March and thats not what I want it to do.
here is what I am trying to do
function addEndDte(){
//alert(isDate(dte.value))
dte=BudgetCopy.BegDte.valu
if(isDate(dte)){
d="";
d=new Date(dte)
d.setMonth(d.getMonth()+4)
d.setDate(31)
document.BudgetCopy.EndDte
}else{
setTimeout("addEndDte()", 200);
}
}
if the month is Feb. and I try to add this d.setDate(31) its going to make the month March and thats not what I want it to do.
so, what you want to do is set the date to the last day of a given month?
instead of this:
d.setMonth(d.getMonth()+4)
d.setDate(31)
do this:
d.setMonth(d.getMonth()+5)
d.setDate(1)
d.setDate( d.getDate()-1 )
instead of this:
d.setMonth(d.getMonth()+4)
d.setDate(31)
do this:
d.setMonth(d.getMonth()+5)
d.setDate(1)
d.setDate( d.getDate()-1 )
or, just this:
d.setMonth(d.getMonth()+5)
d.setDate(0)
d.setMonth(d.getMonth()+5)
d.setDate(0)
ASKER
this d.setDate(31) can actually be anything betwwen 1 and 31 so that would not work
you can do setDate(0) and it will set the date back by 1 .. you can do setDate(99) and it will advance the date by 3 months and 9 days (give or take a few days)
... so advance to the first of the next month, and then go back one day and you land on the last of the previous month :)
Worked for me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you looking for the last day of the month 5 months in advance? if so then
<script>
function lastDay(val){
now=new Date(val)
last=new Date(now.getYear(),now.get Month()+5, 0)
return last
}
d='1/3/2004'
alert(d+' '+lastDay(d))
d='9/30/2004'
alert(d+' '+lastDay(d))
d='10/12/2004'
alert(d+' '+lastDay(d))
</script>
<script>
function lastDay(val){
now=new Date(val)
last=new Date(now.getYear(),now.get
return last
}
d='1/3/2004'
alert(d+' '+lastDay(d))
d='9/30/2004'
alert(d+' '+lastDay(d))
d='10/12/2004'
alert(d+' '+lastDay(d))
</script>
thx for the points :-)
d.setMonth(d.getMonth()+5)
d.setDate(-1)