Node.js examples for Date:Date Calculation
Formats the difference between two dates
Date.timeSince = function ( d0, d1 ) { d1 = d1 || (new Date);// w w w.j ava 2s. com var ms = d1 - d0, delay; var delays = [ { delta : 3.1536e+10, suffix : 'year' }, { delta : 2.592e+9, suffix : 'month' }, { delta : 8.64e+7, suffix : 'day' }, { delta : 3.6e+6, suffix : 'hour' }, { delta : 6e+4, suffix : 'minute' }, { delta : 1000, suffix : 'second' } //anything else is ms ]; while ( delays.length ) { delay = delays.shift() if ( ms >= delay.delta ) { return format( ms / delay.delta, delay.suffix ); } } return format( ms, 'millisecond' ); function format ( interval, suffix ) { interval = Math.floor( interval ); suffix += interval === 1 ? '' : 's'; return interval + ' ' + suffix; } };