JS 计算两个时间戳相差年月日时分秒

JS 计算两个时间戳相差年月日时分秒

//JS 计算两个时间戳相差年月日时分秒
export const calculateDiffTime = (startTime, endTime) => {
  let runTime = parseInt((endTime - startTime) / 1000);
  let year = Math.floor(runTime / 86400 / 365);
  runTime = runTime % (86400 * 365);
  let month = Math.floor(runTime / 86400 / 30);
  runTime = runTime % (86400 * 30);
  let day = Math.floor(runTime / 86400);
  runTime = runTime % 86400;
  let hour = Math.floor(runTime / 3600);
  runTime = runTime % 3600;
  let minute = Math.floor(runTime / 60);
  runTime = runTime % 60;
  let second = runTime;
  let result = {
    year,
    month,
    day,
    hour,
    minute,
    second
  };
  console.log(`相差${year}年${month}月${day}天${hour}小时${minute}分${second}秒`);
  return result;
};

猜你喜欢

转载自blog.csdn.net/guohaosir/article/details/119738751