[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

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

0
vravi22
Asked:
vravi22
  • 3
1 Solution
 
HugoHiaslCommented:
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
0
 
vravi22Author Commented:
I Dont Know how to do it. can you be more clear
0
 
vravi22Author Commented:
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

0
 
HonorGodCommented:
Does this help?

You might want to check out this article.

http://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

0
 
vravi22Author Commented:
I have found my own solution ..! thanks to all ..this is my final script. I have added 1 to the get moth function .

<script type="text/javascript">
function setExpDate(theForm){
      
      var dateString=theForm.txt_lmp.value;
      var myDateParts = dateString.split("/");
      var formDate=myDateParts[1]+"/"+myDateParts[0]+"/"+myDateParts[2];

// set number of days to add
    var interval = 280;
   
    var startDate = new Date(Date.parse(formDate));
    //document.write('LMP: ' + startDate);
    var expDate = startDate;
      if(isNaN(expDate))
      {
            theForm.orderdate2.value="Edd Auto Calculated";
      }
      else{
    expDate.setDate(startDate.getDate() + interval);
    //document.write('<br>EDD: ' + expDate);
      var edd=expDate.getDate()+"/"+(expDate.getMonth()+1)+"/"+expDate.getFullYear();
      
            
      theForm.orderdate2.value=edd;
      }
      // alert(edd);
};


</script>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now