Calculated Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blachca
    New Member
    • Jul 2008
    • 5

    Calculated Date

    I have created a data form in which the user will enter in dates, and I wanted to know if there was a simple script that would allow me to take two date fields in the form and figure the days inbetween/ days left of the two dates. So I want something like a count down that will calculate the days left between the two enterd dates
  • Brosert
    New Member
    • Jul 2008
    • 57

    #2
    i think you cam try something like:
    Code:
    var myDate1=new Date(); //todays date
    var myDate2=newDate(2008,6,6); //older date, you could also use "setFullYear"
    var temp=myDate1.getTime() - myDate2.getTime();
    
    var day=24*60*60*1000; //convert milliseconds to days
    
    var difference = temp/day;

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Hi I hope this code may help you if you are trying get the weekday between two user defined dates. Please ensure that the date format is dd-mon-yyyy.

      [HTML]<Html>
      <head>
      <script language="javas cript">
      function changeVal(month )
      {
      if(month=="jan" ||month=="Jan"| |month=="JAN")
      month=0;
      if(month=="feb" ||month=="Feb"| |month=="FEB")
      month=1;
      if(month=="mar" ||month=="Mar"| |month=="MAR")
      month=2;
      if(month=="apr" ||month=="Apr"| |month=="APR")
      month=3;
      if(month=="may" ||month=="May"| |month=="MAY")
      month=4;
      if(month=="jun" ||month=="Jun"| |month=="JUN")
      month=5;
      if(month=="jul" ||month=="Jul"| |month=="JUL")
      month=6;
      if(month=="aug" ||month=="Aug"| |month=="AUG")
      month=7;
      if(month=="sep" ||month=="Sep"| |month=="SEP")
      month=8;
      if(month=="oct" ||month=="Oct"| |month=="OCT")
      month=9;
      if(month=="nov" ||month=="Nov"| |month=="NOV")
      month=10;
      if(month=="dec" ||month=="Dec"| |month=="DEC")
      month=11;

      return(month);
      }
      function findDiff()
      {
      var day1, day2;
      var month1, month2;
      var year1, year2;

      value1 = document.getEle mentById('stDat e').value;
      value2 = document.getEle mentById('endDa te').value;

      day1 = value1.substrin g (0, value1.indexOf ("-"));
      month1 = value1.substrin g (value1.indexOf ("-")+1, value1.lastInde xOf ("-"));
      month1 = changeVal(month 1);
      year1 = value1.substrin g (value1.lastInd exOf ("-")+1, value1.length);

      day2 = value2.substrin g (0, value2.indexOf ("-"));
      month2 = value2.substrin g (value2.indexOf ("-")+1, value2.lastInde xOf ("-"));
      month2 = changeVal(month 2);
      year2 = value2.substrin g (value2.lastInd exOf ("-")+1, value2.length);

      var startDate = new Date(year1,mont h1,day1);
      var endDate = new Date(year2,mont h2,day2);
      var wrkdays=0;

      while(startDate .getTime()<=end Date.getTime())
      {
      wrkdays++;
      if(startDate.ge tDay()==0)
      {
      wrkdays--;
      }
      else if(startDate.ge tDay()==6)
      {
      wrkdays--;
      }
      else
      {
      }
      var d = new Date(startDate. getTime() + 86400000);
      startDate=d;
      }
      alert("Total Workdays = "+wrkdays);
      }

      </script>
      </head>
      <body>
      <input type="text" id="stDate"><b r/>
      <input type="text" id="endDate"><b r/>
      <input type="button" value="calculat e" onclick="findDi ff()">
      </body>
      </html>[/HTML]

      If you have any doubts pls ask yaar.

      Regards
      Ramanan Kalirajan

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        That code could be shortened considerably, in particular the month code could be one line via an array. Also, the script language attribute is deprecated, use the type attribute instead.

        Comment

        Working...