Link to home
Start Free TrialLog in
Avatar of CFDevHead
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.value;
                              if(isDate(dte)){
                                    d="";
                                    d=new Date(dte)
                                    d.setMonth(d.getMonth()+4)
                                    d.setDate(31)                               
                                    document.BudgetCopy.EndDte.value=twod(d.getMonth()*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.
Avatar of cLFlaVA
cLFlaVA

Why not do this instead?

d.setMonth(d.getMonth()+5)
d.setDate(-1)  
Avatar of knightEknight
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 )

or, just this:

   d.setMonth(d.getMonth()+5)
   d.setDate(0)                          

Avatar of CFDevHead

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
Avatar of GwynforWeb
GwynforWeb
Flag of Canada 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
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.getMonth()+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>
thx for the points :-)