[js] 得到本月、上月、下月的起始、结束日期; 得到今年、去年、明年的开始、结束日期

/**
时间格式化
 */
export function formatDate(val) {
    var date = new Date(Number(val)); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = date.getDate() + " ";
    var h = date.getHours() + ":";
    var m = date.getMinutes() + ":";
    var s = (date.getSeconds() < 10 ? "0" + (date.getSeconds()) : date.getSeconds());
    return Y + M + D + h + m + s;
}
/**
 * @description 得到本月、上月、下月的起始、结束日期
 * @param {String} type 有两种选择,"s"代表开始,"e"代表结束
 * @param {Number} months 不传或0代表本月,-1代表上月,1代表下月
 */
export function getMonth(type) {
  const now = new Date(); // 当前日期
  let nowYear = now.getYear(); // 当前年
  const nowMonth = now.getMonth(); // 当前月
  nowYear += (nowYear < 2000) ? 1900 : 0;  let result;
  if (type === 's') {
    const monthStartDate = new Date(nowYear, nowMonth, 1);
    result = formatDate(monthStartDate);
  } else {
    const monthStartDate = new Date(nowYear, nowMonth, 1);
    const monthEndDate = new Date(nowYear, nowMonth + 1, 1);
    const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    result = formatDate(new Date(nowYear, nowMonth, days));
  }
  return result;
}
/**
 * @description 得到今年、去年、明年的开始、结束日期
 * @param {String} type 有两种选择,"s"代表开始,"e"代表结束
 * @param {Number} dates 不传或0代表今年,-1代表去年,1代表明年
 */
export function getYear(type, dates) {
  const dd = new Date();
  const n = dates || 0;
  const year = dd.getFullYear() + Number(n);
  let day;
  if (type === 's') {
    day = `${year}-01-01`;
  }
  if (type === 'e') {
    day = `${year}-12-31`;
  }
  if (!type) {
    day = `${year}-01-01/${year}-12-31`;
  }
  return day;
}

var  date =  new  Date();

   var  seperator1 =  "-" ;

   var  year = date.getFullYear();

   var  month = date.getMonth() + 1;

   var  strDate = date.getDate();

   if  (month >= 1 && month <= 9) {

     month =  "0"  + month;

   }

   if  (strDate >= 0 && strDate <= 9) {

     strDate =  "0"  + strDate;

   }

   var  currentdate = year + seperator1 + month + seperator1 + strDate;

   return  currentdate;

方法获取格式化时间函数

formatDate (fmt) {
  const date = new Date()
  const o = {
    'Y+': date.getFullYear(),
    'M+': date.getMonth() + 1, // 月
    'D+': date.getDate(), // 日
    'h+': date.getHours(), // 时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    W: date.getDay() // 周
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, () => {
        if (k === 'W') {
          // 星期几
          const week = ['日', '一', '二', '三', '四', '五', '六']
          return week[o[k]]
        } else if (k === 'Y+' || RegExp.$1.length === 1) {
          // 年份 or 小于10不加0
          return o[k]
        } else {
          return ('00' + o[k]).substr(('' + o[k]).length) // 小于10补位0
        }
      })
    }
  }
  return fmt
}

使用
formatDate('YY') // 2022
formatDate('YY-MM')    // 2022-06
formatDate('YY-MM-DD')    // 2022-06-02
formatDate('YY-MM-DD hh:mm:ss')    // 2022-06-02 10:02:23
formatDate('星期W')    // 星期四

1.得到当前(年月日)

getNowMonths() {
      let timeOne = new Date();
      let year = timeOne.getFullYear();
      let month = timeOne.getMonth() + 1;
      let day = timeOne.getDate();
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      const NOW_MONTHS_AGO = `${year}-${month}-${day}`;
      return NOW_MONTHS_AGO;
    }

 2.得到三个月前的(年月日)

getThreeMonths() {
      let timeOne = new Date();
      let year = timeOne.getFullYear();
      let month = timeOne.getMonth() + 1;
      let day = timeOne.getDate();

      // 计算3个月后的月份
      let ThreeMonths = month - 3;

      // 如果小于 0 说明是去年
      if (ThreeMonths <= 0) {
        year = year - 1;
      }
      // 如果 等于 -2 说明当前月是 1 月份 所以三个月前是去年 10月
      if (ThreeMonths === -2) {
        ThreeMonths = 10;
      }
      // 如果 等于 -1 说明当前月是 2 月份 所以三个月前是去年 11月
      if (ThreeMonths === -1) {
        ThreeMonths = 11;
      }
      // 如果 等于 0 说明当前月是 3 月份 所以三个月前是去年 12月
      if (ThreeMonths === 0) {
        ThreeMonths = 12;
      }
      ThreeMonths = ThreeMonths < 10 ? "0" + ThreeMonths : ThreeMonths;

      // 获取当前的时间的日期字符串
      // **如果天数的值为零,则默认返回当前月份的最后一天
      let timeTow = new Date(year, ThreeMonths, 0);
      // 获取三个月前的最后一天
      let ThreeMonthsDay = timeTow.getDate();
      // 判断如果当前月份的天数大于三个月前的天数时,则当前天数等于三个月前的天数
      if (day > ThreeMonthsDay) {
        day = ThreeMonthsDay;
      }
      day = day < 10 ? "0" + day : day;
      // 格式化时间
      // const THREE_MONTHS_AGO = `${year}/${ThreeMonths}/${day}`

      // 生成时间戳  需要的话做
      //const THREE_STAMP = new Date(THREE_MONTHS_AGO).getTime()
      const THREE_STAMP = `${year}-${ThreeMonths}-${day}`;
      return THREE_STAMP;
    },

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值