1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| calculateDiffTime(startTime, endTime, type) { var runTime = parseInt(endTime - startTime) var year = Math.floor(runTime / 86400 / 365) runTime = runTime % (86400 * 365) var month = Math.floor(runTime / 86400 / 30) runTime = runTime % (86400 * 30) var day = Math.floor(runTime / 86400) runTime = runTime % 86400 var hour = Math.floor(runTime / 3600) runTime = runTime % 3600 var minute = Math.floor(runTime / 60) runTime = runTime % 60 var second = runTime if (type === 1) { return year + '年' } else if (type === 2) { return year + '年' + month + '月' } else if (type === 3) { return year + '年' + month + '月' + day + '日' } else { return year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + second + '秒' } },
|
由以上代码,可计算两个时间戳段相差的年月日时分秒的具体数字,方便使用,如前端显示字段服务期限:1年
若本文有帮助到阅读本文的同学,欢迎点赞、关注、收藏,互相学习交流。