首页 > 其他 > 详细

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

时间:2019-10-10 10:12:38      阅读:74      评论:0      收藏:0      [点我收藏+]
//获取当前时刻的时间
// 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;
}

  






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

原文:https://www.cnblogs.com/luoxuemei/p/11646108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!