项目开发过程中,一些常用的方法

//获取当前时刻的时间
// type = 1年月日,type=2时分秒,fommatter="-"表示年月日用-隔开,否则用"/"隔开export function curTimeFun(type,fommatter) {  const myDate = new Date();  const year = myDate.getFullYear()>9?myDate.getFullYear():'0'+myDate.getFullYear();  const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:'0'+(myDate.getMonth() + 1);  const date = myDate.getDate()>9?myDate.getDate():'0'+myDate.getDate();  const h = myDate.getHours()>9?myDate.getHours():'0'+myDate.getHours();       //获取当前小时数(0-23)  const m = myDate.getMinutes()>9?myDate.getMinutes():'0'+myDate.getMinutes();     //获取当前分钟数(0-59)  const s = myDate.getSeconds()>9?myDate.getSeconds():'0'+myDate.getSeconds();  let nowTime = "";  if(type == '1'){    if(fommatter == '-') {      nowTime = year + '-' + month + "-" + date;    } else {      nowTime = year + '/' + month + "/" + date;    }  } else if(type == '2'){    nowTime = h + ':' + m + ":" + s;  } else {    if(fommatter == '-') {      nowTime = year + '-' + month + "-" + date + " " + h + ':' + m + ":" + s;    } else {      nowTime = year + '/' + month + "/" + date + " " + h + ':' + m + ":" + s;    }  }  return nowTime;}

  

// 获取今天星期几
export function getWeekFun() {  const weekDay = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']  let week = new Date().getDay();  return weekDay[week];}

  

// 查询最近n年,n表示前多少年的意思
// 例如查询近5年的时间,n=4,不包括今年的的前4年
export function getLastNYear(n) {  const myDate = new Date;  const curYear = myDate.getFullYear();  if(n =='' || n==undefined || n == null){    n =0;  }  let rstYear = curYear*1 - n*1;  return rstYear;}

  

// 查询最近n月,n表示前多少月的意思
export function getLastNMonth(n) {  const myDate = new Date;  const curYear = myDate.getFullYear();  const curMonth = myDate.getMonth()+1; // 月份从0开始算起。需要加1  if(n =='' || n==undefined || n == null){n =0;}  let rstYear = '';  let rstMonth = '';  if(n>curMonth){ //表示去到去年的月份,年份需要去到上一年    rstYear = curYear*1-1*1;    rstMonth = 12-(n-curMonth)+1;  } else {    rstYear =curYear;    rstMonth = curMonth -n;  }  rstMonth = (rstMonth)>9?rstMonth:'0'+(rstMonth);  let rstYearMonth = rstYear + '-' + rstMonth;  return rstYearMonth;}

  

 

// 获取最近n天的时间,n表示前多少天的意思。// 例如查询近7天的时间,n=6,不包括当天的的前6天
export function getLastNDate(n,fommatter) {  const d = new Date();  var myDate=new Date(d.getTime()-86400000*n);  // 获取前n天的日期  const year = myDate.getFullYear()>9?myDate.getFullYear():'0'+myDate.getFullYear();  const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:'0'+(myDate.getMonth() + 1);  const date = myDate.getDate()>9?myDate.getDate():'0'+myDate.getDate();  let last7Date = '';  if(fommatter == '-') {    last7Date = year + '-' + month + "-" + date;  } else {    last7Date = year + '/' + month + "/" + date;  }  return last7Date;}

  

// 获取最近的n个小时,n表示前多少小时的意思。
export function getLastNHour(n,fommatter) {  const d = new Date();  var myDate=new Date(d.getTime()-86400000*n);  // 获取前n天的日期  const year = myDate.getFullYear()>9?myDate.getFullYear():'0'+myDate.getFullYear();  const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:'0'+(myDate.getMonth() + 1);  const date = myDate.getDate()>9?myDate.getDate():'0'+myDate.getDate();  const h = myDate.getHours()>9?myDate.getHours():'0'+myDate.getHours();       //获取当前小时数(0-23)  const m = myDate.getMinutes()>9?myDate.getMinutes():'0'+myDate.getMinutes();     //获取当前分钟数(0-59)  const s = myDate.getSeconds()>9?myDate.getSeconds():'0'+myDate.getSeconds();  let nowTime = "";  if(fommatter == '-') {    nowTime = year + '-' + month + "-" + date + " " + h + ':' + m + ":" + s;  } else {    nowTime = year + '/' + month + "/" + date + " " + h + ':' + m + ":" + s;  }  return nowTime;}



计算机