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


Comments ( 1 Comment )

ssdfd…

Finding Difference Between Dates in Javascript | Alpha Papa Hotel…

ssdfd added these pithy words on Sep 26 14 at 10:09 am

Add a Comment

You must be logged in to post a comment.


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