johnharvey Posted June 20, 2012 Share Posted June 20, 2012 Hello, How to subtract [mm/dd/yy] format to another [mm/dd/yy] format and get X number of days? Here is my code that im trying to figure out: <html><head><title></title></head><body><script language="javascript">function subtractdate(form){ var a = form.datetextbox1.value; var b = form.datetextbox2.value; var datedifference = parseFloat(a) - parseFloat( + " days difference"; // <--i think this is not correct document.getElementById('result').innerHTML = datedifference;}</script><form>ie. [dd] - [mm/dd/yy] = X days difference<br><br><input type=text name="datetextbox1" onchange="subtractdate(this.form);">-<input type=text name="datetextbox2" onchange="subtractdate(this.form);"><div id="result"></div></form></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted June 21, 2012 Share Posted June 21, 2012 You should use a Date object instead of converting the date string to a float: http://www.w3schools.com/js/js_obj_date.asphttp://www.w3schools.com/jsref/jsref_obj_date.asp Once you have the 2 Date objects then you can use their valueOf methods to get the Unix timestamp, which will be the number of milliseconds since 1/1/70. You subtract the two and get the number of milliseconds between them, and divide that by the number of milliseconds in a day to get the number of days. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now