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);
}
}