How to subtract two dates.

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2007
Posts: 177
Reputation: Kusno is an unknown quantity at this point 
Solved Threads: 14
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

How to subtract two dates.

 
0
  #1
Aug 22nd, 2008
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 ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script LANGUAGE="JavaScript">
  2. var b;
  3. b = 0;
  4. function countDown()
  5. {
  6. var id;
  7. var now = new Date();
  8. var lastUpdate = new Date(form1.TxtDate.value);
  9. var days = Math.floor(((lastUpdate - now) / (60*60*24)) / 1000);
  10.  
  11. if(days==1)
  12. {
  13. form1.TxtDays.value = "Your password will expire in " + days + " day !!!";
  14. }
  15. else
  16. if(days > 1 && days <= 10)
  17. {
  18. form1.TxtDays.value = "Your password will expire in " + days + " days !!!";
  19. }
  20. else
  21. {
  22. form1.TxtDays.value = "";
  23. clearTimeout(id);
  24. }
  25.  
  26. if(b==0)
  27. {
  28. b = 1;
  29. }
  30. else
  31. {
  32. b = 0;
  33. form1.TxtDays.value = "";
  34. }
  35. id = window.setTimeout("countDown();",500);
  36. }
  37. </script>
NEVER NEVER NEVER GIVE UP
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to subtract two dates.

 
1
  #2
Aug 22nd, 2008
Maybe this might get you working in the right direction:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--
  2. Calculate the difference between the two dates.
  3. Copyright (C) 2008 sos aka Sanjay
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. -->
  18. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  19. "http://www.w3.org/TR/html4/strict.dtd">
  20. <html>
  21. <head>
  22. <meta http-equiv="Script-Content-Type" content="text/javascript">
  23. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  24. <title>Date examples</title>
  25. <script type="text/javascript">
  26. // Error checking kept to a minimum for brevity
  27.  
  28. function setDifference(frm) {
  29. var dtElem1 = frm.elements['dtTxt1'];
  30. var dtElem2 = frm.elements['dtTxt2'];
  31. var resultElem = frm.elements['resultTxt'];
  32.  
  33. // Return if no such element exists
  34. if(!dtElem1 || !dtElem2 || !resultElem) {
  35. return;
  36. }
  37.  
  38. //assuming that the delimiter for dt time picker is a '/'.
  39. var x = dtElem1.value;
  40. var y = dtElem2.value;
  41. var arr1 = x.split('/');
  42. var arr2 = y.split('/');
  43.  
  44. // If any problem with input exists, return with an error msg
  45. if(!arr1 || !arr2 || arr1.length != 3 || arr2.length != 3) {
  46. resultElem.value = "Invalid Input";
  47. return;
  48. }
  49.  
  50. var dt1 = new Date();
  51. dt1.setFullYear(arr1[2], arr1[1], arr1[0]);
  52. var dt2 = new Date();
  53. dt2.setFullYear(arr2[2], arr2[1], arr2[0]);
  54.  
  55. resultElem.value = (dt2.getTime() - dt1.getTime()) / (60 * 60 * 24 * 1000);
  56. }
  57. </script>
  58. </head>
  59. <body>
  60. <form id="frm" action="#">
  61. <div id="container">
  62. <input type="text" name="dtTxt1" id="dtTxt1" value="21/12/2004">
  63. <br>
  64. <input type="text" name="dtTxt2" id="dtTxt2" value="27/12/2004">
  65. <br>
  66. <input type="text" name="resultTxt" id="resultTxt">
  67. <br><br>
  68. <input type="button" value="Calculate" onclick="setDifference(this.form);">
  69. </div>
  70. </form>
  71. </body>
  72. </html>
Last edited by ~s.o.s~; Aug 22nd, 2008 at 5:05 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC