1、两日期天数差
getDiffDate(day1,day2){let date1 = new Date(day1);let date2 = new Date(day2);date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());const diff = date1.getTime() - date2.getTime(); //计算出相差的时间戳const diffDate = diff / (24 * 60 * 60 * 1000); //计算相关的天数return diffDate;},
2、某一日期前推后推固定天数后的日期
getNewData(dateTemp, days,type) { var nDate = ''if (JSON.stringify(dateTemp).includes('-')) {nDate = new Date(dateTemp)} else {nDate = dateTemp}var millSeconds = ''if (type == 1) { millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000); } else {millSeconds = Math.abs(nDate) - (days * 24 * 60 * 60 * 1000); } var rDate = new Date(millSeconds); var year = rDate.getFullYear(); var month = rDate.getMonth() + 1; if (month < 10) month = "0" + month; var date = rDate.getDate(); if (date < 10) date = "0" + date; return (year + "-" + month + "-" + date); },
3、时间格式化
formatTime(time, format) {var time = time ? new Date(time) : new Date();var year = time.getFullYear();var month = (time.getMonth() + 1).toString().length == 1 ? "0" + (time.getMonth() + 1).toString() : (time.getMonth() + 1);var day = time.getDate().toString().length == 1 ? "0" + time.getDate() : time.getDate();var hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();var m = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();var result = format.replace('YYYY', year).replace('MM', month).replace('dd', day).replace('HH', hour).replace('mm', m).replace('ss', s);return result; },