JS时间计算

归云
归云
发布于 2023-04-07 / 1644 阅读
0
0

JS时间计算

#js

JS时间计算

function calcDate(dateStr,num) {
  // 获取日期
  const date = new Date(dateStr);
  // 增加20天
  date.setDate(date.getDate() + num);
  // 获取增加后的年月日
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  // 返回结果
  return `${year}年${month}月${day}日`;
}

// 调用函数
const result = calcDate('2023/03/27',20);
console.log(result); // 预计输出结果:2023年4月16日

评论