SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Location
    Bangladesh
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help about calculate amount of day between two dates

    Hi all,

    I need help about calculate amount of day between two dates. I want to do this in such a way:

    There are two input date field from which i select date then number of days between selected date will be shown below. The code snippet look like such below:

    Date1: <input type="text" name="fromdate" id="fromdate" value="" style="width:120px;" onclick="displayCalendar(document.add_event.fromdate,'yyyy-mm-dd',this)">&nbsp;<font color="#8f0000">*</font>
    <IMG onclick="displayCalendar(document.add_event.fromdate,'yyyy-mm-dd',this)" style="cursorointer;" src="<?=$_path_main?>/SiteImages/cal.gif" alt="Pick a date" width="16" height="16" hspace="4" vspace="4" border="0" align="absmiddle">
    <span class="Text10BlackBold">yyyy-mon-dd</span>

    Date2: <input type="text" name="todate" id="todate" value="" style="width:120px;" onclick="displayCalendar(document.add_event.todate,'yyyy-mm-dd',this)">&nbsp;<font color="#8f0000">*</font>
    <IMG onclick="displayCalendar(document.add_event.todate,'yyyy-mm-dd',this)" style="cursorointer;" src="<?=$_path_main?>/SiteImages/cal.gif" alt="Pick a date" width="16" height="16" hspace="4" vspace="4" border="0" align="absmiddle">
    <span class="Text10BlackBold">yyyy-mon-dd</span>

    Total days: (Here total days between selected days will display automatically before clicking save button finally)

    Can anyone please help me about this how can i do it or is it possible?

    Thanks,
    Rima.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Days(frm,f,t,ip){
     var f=frm[f].value.split(/\D/),t=frm[t].value.split(/\D/),days,z0;
     if (f.length==3&&t.length==3&&f.join('')<t.join('')){
      days=new Date(f[0],f[1],1,-1).getDate()-f[2];
      t[0]-f[0]
      for (z0=f[1]*1+1;z0<t[1]*1+(t[0]-f[0])*12;z0++){
       days+=new Date(f[0],z0,1,-1).getDate();
      }
      frm[ip].value=days+t[2]*1;
     }
    }
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <form>
    <input name="from" value="2012-02-27" />
    <input name="to" value="2012-05-02" />
    <input type="button" name="" value="test" onmouseup="Days(this.form,'from','to','days');" />
    <input name="days" />
    </form>
    
    </body>
    
    </html>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Wow, that's a highly complicated way of doing things.

    Let's simplify it to this instead:

    Code javascript:
    function calculateDays() {
        var form = this.form,
            from = form.elements.from,
            to = form.elements.to,
            days = form.elements.days,
            seconds = (new Date(to.value) - new Date(from.value)) / 1000;
        days.value = Math.floor(seconds / 60 / 60 / 24);
    }

    and in a sample test page:

    HTML Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title></title>
    </head>
    <body>
    <form id="dateDifference">
    	<input name="from" value="2012-02-27">
    	<input name="to" value="2012-05-02">
    	<input type="button" id="test" value="test">
    	<input name="days">
    </form>
    <script>
    function calculateDays() {
        var form = this.form,
            from = form.elements.from,
            to = form.elements.to,
            days = form.elements.days,
            seconds = (new Date(to.value) - new Date(from.value)) / 1000;
        days.value = Math.floor(seconds / 60 / 60 / 24);
    }
    
    var button = document.getElementById('test');
    button.onclick = calculateDays;
    </script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Location
    Bangladesh
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks paul_wilkins.

    It is useful and i got expected result using this code.

    ---
    Rima

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Location
    Bangladesh
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vwphillips,

    It doesn't work properly.

    Thanks,
    Rima.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •