Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crudeboy/95c2e30f7d8613333b69e8f3c7f83d69 to your computer and use it in GitHub Desktop.
Save crudeboy/95c2e30f7d8613333b69e8f3c7f83d69 to your computer and use it in GitHub Desktop.
// least time differnce in an array
function minDiff(arr){
var sortedArr = arr.sort();
var diff = Number.MAX_VALUE;
for (let i = 0; i < sortedArr.length - 1; i++){
if(Math.abs(new Date('01/01/2001 ' + sortedArr[i + 1]) - new Date('01/01/2001 ' +sortedArr[i]) < diff )){
diff = new Date('01/01/2001 ' + sortedArr[i + 1]) - new Date('01/01/2001 ' +sortedArr[i])
}
}
return diff / (1000 * 60)
}
console.log(minDiff(['16:00','12:20','16:15']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment