| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 | export const calcDate = (date1, date2) => {  let date3 = date2 - date1;  let days = Math.floor(date3 / (24 * 3600 * 1000))  let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数  let hours = Math.floor(leave1 / (3600 * 1000))  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数  let minutes = Math.floor(leave2 / (60 * 1000))  let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数  let seconds = Math.round(date3 / 1000)  return {    leave1,    leave2,    leave3,    days: days,    hours: hours,    minutes: minutes,    seconds: seconds,  }}/** * 日期格式化 */export function dateFormat(date, format) {  format = format || 'yyyy-MM-dd hh:mm:ss';  if (date !== 'Invalid Date') {    let o = {      "M+": date.getMonth() + 1, //month      "d+": date.getDate(), //day      "h+": date.getHours(), //hour      "m+": date.getMinutes(), //minute      "s+": date.getSeconds(), //second      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter      "S": date.getMilliseconds() //millisecond    }    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,      (date.getFullYear() + "").substr(4 - RegExp.$1.length));    for (let k in o)      if (new RegExp("(" + k + ")").test(format))        format = format.replace(RegExp.$1,          RegExp.$1.length === 1 ? o[k] :          ("00" + o[k]).substr(("" + o[k]).length));    return format;  }  return '';}/** * 获取上月第一天和下月最后一天 * type 1为 [yyyy-MM-dd,yyyy-MM-dd] * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59] * type 4为 [yyyy-MM-dd,yyyy-MM-dd] 当月的第一天和最后一天 * type为空默认2 */export function defaultDate(type) {  type = type ? type : 2  const date = new Date();  const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);  const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);  if (type == 1) {    return [      dateFormat(startDate, "yyyy-MM-dd"),      dateFormat(endDate, "yyyy-MM-dd")    ];  }else if (type == 3){    return[      dateFormat(new Date(date.getFullYear(), date.getMonth()-1, 1), "yyyy-MM-dd"),      dateFormat(new Date((date.getMonth() + 1) === 12?date.getFullYear()+1:date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")    ];  } else if (type == 4){    return[      dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"),      dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd")    ];  }else {    return [      dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",      dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"    ];  }}/** * 获取当天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate2() {  const date = new Date();  return [    dateFormat(date, "yyyy-MM-dd"),    dateFormat(date, "yyyy-MM-dd")  ];}/** * 获取本月第一天和本月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */export function defaultDate3() {  const date = new Date();  const startDate = new Date(date.getFullYear(), date.getMonth(), 1);  const endDate = new Date(date.getFullYear(), date.getMonth()+1, 0);  return [    dateFormat(startDate, "yyyy-MM-dd"),    dateFormat(endDate, "yyyy-MM-dd")  ];}/** * 获取当年 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate4() {  const date = new Date();  return dateFormat(date, "yyyy");}/** * 获取上月第一天和上月最后一天 * [yyyy-MM-dd,yyyy-MM-dd] */ export function defaultDate5() {  const date = new Date();  const startDate = new Date(date.getFullYear(), date.getMonth()-1, 1);  const endDate = new Date(date.getFullYear(), date.getMonth(), 0);  return [    dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",    dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"  ];}// 获得当前日期export function getCurrentDate(type = 'dateTime') {  const date = new Date();  const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());  if (type == 'date') {    return dateFormat(currentDate, 'yyyy-MM-dd')  } else if (type == 'dateTime') {    return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')  }}export function getYearDate(){  const date = new Date();  return date.getFullYear()}/** * 获取 n 天后的日期 * yyyy-MM-dd */export const NdayDate = (num) => {  let date1 = new Date();  //今天时间  let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();  let date2 = new Date(date1);  date2.setDate(date1.getDate() + num);  //num是正数表示之后的时间,num负数表示之前的时间,0表示今天  let time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate());  return time2;}// 补零方法function addZero(num) {  if (parseInt(num) < 10) {    num = '0' + num;  }  return num;}
 |