Skip to content

Instantly share code, notes, and snippets.

@davidraedev
Created January 25, 2017 04:57
Show Gist options
  • Save davidraedev/5539dfafb7903ff736b1d1430ad4aaf1 to your computer and use it in GitHub Desktop.
Save davidraedev/5539dfafb7903ff736b1d1430ad4aaf1 to your computer and use it in GitHub Desktop.
adafruit coupon days between
var dates = [
"2017-01-11",
"2017-01-04",
"2016-12-21",
"2016-12-07",
"2016-11-03",
"2016-10-27",
"2016-10-13",
"2016-09-22",
"2016-09-15",
"2016-08-25",
"2016-08-18",
"2016-08-11",
"2016-08-04",
"2016-07-21",
"2016-07-07",
"2016-06-23",
"2016-06-09",
"2016-06-02",
];
function daysBetween( date1, date2 ) {
var day_millis = ( 1000 * 60 * 60 * 24 );
var parts = date1.split( "-" );
var firstDate = new Date( parts[0], parts[1], parts[2] );
var parts = date2.split( "-" );
var secondDate = new Date( parts[0], parts[1], parts[2] );
return Math.round( Math.abs( ( firstDate.getTime() - secondDate.getTime() ) / day_millis ) );
}
var avg = 0;
for ( var i = 0; i < dates.length; i++ ) {
if ( i === 0 )
continue;
var days = daysBetween( dates[ i ], dates[ i - 1 ] );
avg += days;
console.log( days );
}
console.log( "Average: "+ Math.round( avg / dates.length ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment