Skip to content Skip to sidebar Skip to footer

Display Real Time Years, Months, Weeks And Days Between 2 Days In Javascript

This is what I've coded it up, and it appears to work. window.onload = function() { var currentSpan = document.getElementById('current'); var minute = 60000, hour = m

Solution 1:

You could put those constants in an array and then just iterate through it:

functiontdiff(utc) {
  var diff = newDate() - newDate(utc);
  var units = [
    1000 * 60 * 60 * 24 * 365,
    1000 * 60 * 60 * 24 * 28,
    1000 * 60 * 60 * 24 * 7,
    1000 * 60 * 60 * 24,
    1000 * 60 * 60,
    1000 * 60,
    1000
  ];

  var rv = [];
  for (var i = 0; i < units.length; ++i) {
    rv.push(Math.floor(diff / units[i]));
    diff = diff % units[i];
  }
  return rv;
}

Of course since months and years aren't always the same length, this isn't really that accurate, but I figure you realize that :-)

Also see this: http://timeago.yarp.com/ it's kind-of cool

Solution 2:

You don't have to set up any constants, but if you need all those numbers (years, months, days, hours etc) I don't know of any other way it can be done.

Post a Comment for "Display Real Time Years, Months, Weeks And Days Between 2 Days In Javascript"