Finding Difference Between Dates in Javascript

This snippet of code lets you determine the difference (in weeks, days, hours, minutes, or seconds) between two dates. For the example below I’m using the current date as one of the two.


var future = new Date("01/01/2020");
var today=new Date();

var weeks = 1000 * 60 * 60 * 24 * 7;
var days = 1000 * 60 * 60 * 24;
var hours = 1000 * 60 * 60;
var mins = 1000 * 60;
var secs = 1000;

var diffWeeks = Math.ceil((today.getTime()-future.getTime())/(weeks));
var diffDays = Math.ceil((today.getTime()-future.getTime())/(days));
var diffHours = Math.ceil((today.getTime()-future.getTime())/(hours));
var diffMinutes = Math.ceil((today.getTime()-future.getTime())/(mins));
var diffSeconds = Math.ceil((today.getTime()-future.getTime())/(secs));

Subscribe to comments Comment | Trackback |
Post Tags: ,

Browse Timeline


Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


© Copyright 2009-2010 Alpha Papa Hotel . Thanks for visiting!