javaScript常用日期计算函数
1.计算两段时间的间隔 并取整
computedDays(start, end) {
if (start && end) {
const day =
(new Date(end).getTime() - new Date(start).getTime()) /
3600 /
1000 /
24;
return parseInt(day);
}
},
2.用于判断结束时间是否大于开始时间
computedSrartIsBigEnd(start, end) {
if (start && end) {
if (new Date(end).getTime() - new Date(start).getTime() > 0) {
return true;
} else {
return false;
}
}
},
3.根据当前日期向后推导n天
nextDateByNum(date,n){
const nowTime = new Date(date);
nowTime.setTime(nowTime.getTime() + 3600 * 1000 * 24 * n);
return nowTime
}
4.用于时间日期的格式化(月日不足两位自动补0)
handleData(date) {
const timeDate = new Date(date);
const year = timeDate.getFullYear();
const month = (timeDate.getMonth() + 1).toString().padStart(2, 0);
const day = timeDate.getDate().toString().padStart(2, 0);
const hour = timeDate.getHours().toString().padStart(2, 0);
const min = timeDate.getMinutes().toString().padStart(2, 0);
const s = timeDate.getSeconds().toString().padStart(2, 0);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + s;
},