Link to home
Start Free TrialLog in
Avatar of vravi22
vravi22Flag for India

asked on

add days to date javascript,php

Hello Experts  I have a from where I am required to add 280 days to a date selected from calendar.I have  found a javascript function  to do this . the only  problem here  is that the year format is dd/mm/yy where as i need a dd/mm/yyyy  .when i enter
 06/03/09     i get 11/12/09    but wen i enter
 06/03/2009 i get 11/12/109 .
i have removed the jquery calendar as it is irrelevant .
 I need to fix this today. here is my code.
<html>

<body>

<script language="javascript">

function PopulateToDate(){

if (document.forms[0].from.value!='')

     {

     var FromDate =document.forms[0].from.value;

     var DateSplit =FromDate.split('/');

     var FromDate   =new Date(DateSplit[2],DateSplit[1]-1,DateSplit[0]);

     FromDate.setDate(FromDate.getDate()+280);

     var TheDate = new Array(3)

     TheDate[1] = new String(FromDate.getDate())

     TheDate[2] = new String(FromDate.getMonth()+1)

     TheDate[3] = new String(FromDate.getYear())

     for(var x = 1; x < TheDate.length; x++)

          {

          if (TheDate[x].length == 1)

          TheDate[x] = "0" + TheDate[x]

          }

     document.forms[0].to.value=TheDate[1]+'/'+(TheDate[2])+'/'+TheDate[3];

     }

}

</script>



<form name="form1">

  From Date

  <img src="no.gif" width="85" height="1">

  To Date

  <br>

  <input type="text" name="from" onblur="PopulateToDate()" size="20">

  <input type="text" name="to" size="20">

</form>

</body>

</html>

Open in new window

Avatar of HugoHiasl
HugoHiasl

getYear() returns the number of years based on the year 1900. If you enter 2009 it will return 109 which is correct.

You need to handle this by yourself
Avatar of vravi22

ASKER

I Dont Know how to do it. can you be more clear
Avatar of vravi22

ASKER

I have found the code for the Y2K problem  as well .since i m new to javascript,I dunno how to fix it. here is the jscript function that has the y2k fix  that I need to add .I just can't figure out where to add this .
 
<script language="JavaScript"><!--

function addDays(myDate,days) {

    var datearray = myDate.split('/');

     var d3 = new Date(datearray[2],datearray[1]-1,datearray[0]);    

     var newdate = new Date(d3.getTime() + days*24*60*60*1000);

     var dd = newdate.getDate();

     var mm = newdate.getMonth()+1; // 0 based

     var yy = newdate.getYear();

    if (yy < 1000) yy +=1900; // Y2K fix

    return (dd + '/' + mm +'/' + yy);

}



alert(addDays('06/03/2009',280));

//-->

</script>

Open in new window

Avatar of HonorGod
Does this help?

You might want to check out this article.

https://www.experts-exchange.com/articles/Programming/Languages/Scripting/JavaScript/Can-I-have-a-date.html
<html>
<body>

<script type='text/javascript'>
  function addDays(myDate,days) {
    var datearray = myDate.split('/');
    //----------------------------------------
    // This presumes date string in form: DD/MM/YYYY
    // Is that what you want?
    //----------------------------------------
    var d3 = new Date( datearray[ 2 ],
                       datearray[ 1 ] - 1,
                       datearray[ 0 ]
                     );
    alert( 'Start: ' + myDate + ' = ' + d3 )
    var newdate = d3;
    newdate.setDate( newdate.getDate() + days );
//  var newdate = new Date(d3.getTime() + days*24*60*60*1000);
    
    var dd = newdate.getDate();
    var mm = newdate.getMonth()+1; // 0 based
    var yy = newdate.getFullYear();

    return ( dd + '/' + mm + '/' + yy );
  }
  alert( addDays( '8/1/2010', 180 ) )
</script>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of vravi22
vravi22
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