| | |
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 |
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 ?
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)
<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>
NEVER NEVER NEVER GIVE UP
Maybe this might get you working in the right direction:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!-- 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>
Last edited by ~s.o.s~; Aug 22nd, 2008 at 5:05 am.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Tutorial: Handling Dates and Time in Delphi (Pascal and Delphi)
- how to calculate the days between two dates excluding saturday and sunday (JavaScript / DHTML / AJAX)
- Script to compare dates? (Shell Scripting)
- calculate difference of 2 dates (JavaScript / DHTML / AJAX)
- DATETIME data type arithmenic in SQL Server 2005 (MS SQL)
- calendar control (VB.NET)
- need urgent help for a query (MS SQL)
- Need help With this C++ Program..Confused.. (C++)
- date difference in c++ (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: how to know that a perticular website using ajax or not?
- Next Thread: Google API Search Engine Code
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c website window windowofwords windowsxp wysiwyg \n






