Skip to content

Instantly share code, notes, and snippets.

@axetroy
Created April 25, 2017 02:32
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 axetroy/64bd521766688d927be948a38b97af55 to your computer and use it in GitHub Desktop.
Save axetroy/64bd521766688d927be948a38b97af55 to your computer and use it in GitHub Desktop.
计算两个时间点的时间差
function diffTime(time1) {
return function(time2) {
let seconds = Math.abs(time1 - time2) / 1000;
const days = Math.floor(seconds / (3600 * 24));
seconds = seconds - days * 3600 * 24;
const hours = Math.floor(seconds / 3600);
seconds = seconds - hours * 3600;
const minutes = Math.floor(seconds / 60);
seconds = seconds - minutes * 60;
return {
days,
hours,
minutes,
seconds: parseInt(seconds)
};
};
}
@axetroy
Copy link
Author

axetroy commented Apr 25, 2017

diffTime(new Date('2016-01-01'))(new Date('2017-01-01'))
// Object {days: 366, hours: 0, minutes: 0, seconds: 0}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment