Skip to content

Instantly share code, notes, and snippets.

@iwinux
Created December 20, 2011 17:24
Show Gist options
  • Save iwinux/1502393 to your computer and use it in GitHub Desktop.
Save iwinux/1502393 to your computer and use it in GitHub Desktop.
function inMinutes(time) {
return time.getHours() * 60 + time.getMinutes();
}
function calcDuration(timeStart, timeEnd) {
if (!timeStart || !timeEnd) {
return "";
}
var start = inMinutes(timeStart);
var end = inMinutes(timeEnd);
var result = "";
if (end < start) {
end += 24 * 60;
}
var offset = end - start;
if (offset >= 60) {
var hour = Math.floor(offset / 60);
var minute = offset % 60;
if (minute > 0) {
result = hour + "小时" + minute + "分钟";
} else {
result = hour + "小时";
}
} else {
result = offset + "分钟";
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment