View Single Post
Old 02-27-2013, 02:51 AM   PM User | #1
JonBMN
New Coder

 
Join Date: Feb 2013
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
JonBMN is an unknown quantity at this point
Trying to get the days between dates.

no regexp please. I feel my logic is sound on this but I'm definitely doing something wrong. Is it a logic error on my part or am I not passing Date.parse with the right value. I'm not sure please help! thank you for your replies!

Code:
//days between function only yyyy-mm-dd will work.
function daysBetween(firstd, secondd, string) {
	var date1 = firstd,
		date1 = Date.parse(date1),
		date2 = secondd,
		date2 = Date.parse(date2),
		str = string
		;
	if (str === "days") {
		var day = 24 * 60 * 60 * 1000; //hours times minutes times seconds times milliseconds
		date1ms = date1.getTime();	//we convert both dates to milliseconds.
		date2ms = date2.getTime();
		var thedifference = Math.abs(date1ms - date2ms);
		return Math.round(thedifference / day);
	}
	else if (str === "hours") {
		var hour = 60 * 60 * 1000;
		date1ms = date1.getTime();
		date2ms = date2.getTime();
		var thedifference = Math.abs(date1ms - date2ms);
		return Math.round(thedifference / hour);
	}
}

Last edited by JonBMN; 02-27-2013 at 04:05 AM..
JonBMN is offline   Reply With Quote