Dear all,

I have a problem. I want to subtract two dates use JavaScript.

First date source is from SQL database, I format it use ASP.Net code to "yyyy-MM-dd"
without time and save it into TxtDate.
The second date is from current date.

I want subtract those two dates without entering time of those dates.
The current date currently still has complete date format; "Fri Aug 22 10:06:38 UTC+0700 2008".

So how do I format that current date to "yyyy-MM-dd" format ?

<script LANGUAGE="JavaScript">     
  var b;
  b = 0;
  function countDown() 
  { 
      var id;
      var now = new Date(); 
      var lastUpdate = new Date(form1.TxtDate.value); 
      var days = Math.floor(((lastUpdate - now) / (60*60*24)) / 1000); 
      
      if(days==1)
      {
         form1.TxtDays.value = "Your password will expire in " + days + " day !!!";
      }            
      else
      if(days > 1 && days <= 10)
      {
         form1.TxtDays.value = "Your password will expire in " + days + " days !!!";
      }
      else
      {
         form1.TxtDays.value = "";
         clearTimeout(id);
      } 
      
      if(b==0)
      {
         b = 1;
      }
      else
      {
         b = 0;
         form1.TxtDays.value = "";
      }
      id = window.setTimeout("countDown();",500);        
  } 
  </script>

Maybe this might get you working in the right direction:

<!--
    Calculate the difference between the two dates.
    Copyright (C) 2008  sos aka Sanjay

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Script-Content-Type" content="text/javascript">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Date examples</title>
  <script type="text/javascript">
  // Error checking kept to a minimum for brevity
  
  function setDifference(frm) {
    var dtElem1 = frm.elements['dtTxt1'];
    var dtElem2 = frm.elements['dtTxt2'];
    var resultElem = frm.elements['resultTxt'];
    
    // Return if no such element exists
    if(!dtElem1 || !dtElem2 || !resultElem) {
      return;
    }
    
    //assuming that the delimiter for dt time picker is a '/'.
    var x = dtElem1.value;
    var y = dtElem2.value;
    var arr1 = x.split('/');
    var arr2 = y.split('/');
    
    // If any problem with input exists, return with an error msg
    if(!arr1 || !arr2 || arr1.length != 3 || arr2.length != 3) {
      resultElem.value = "Invalid Input";
      return;
    }

    var dt1 = new Date();
    dt1.setFullYear(arr1[2], arr1[1], arr1[0]);
    var dt2 = new Date();
    dt2.setFullYear(arr2[2], arr2[1], arr2[0]);
    
    resultElem.value = (dt2.getTime() - dt1.getTime()) / (60 * 60 * 24 * 1000);
  }
  </script>
</head>
<body>
<form id="frm" action="#">
  <div id="container">
  <input type="text" name="dtTxt1" id="dtTxt1" value="21/12/2004">
  <br>
  <input type="text" name="dtTxt2" id="dtTxt2" value="27/12/2004">
  <br>
  <input type="text" name="resultTxt" id="resultTxt">
  <br><br>
  <input type="button" value="Calculate" onclick="setDifference(this.form);">
  </div>
</form>
</body>
</html>
commented: Thanks a lot !!! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.