vue 常用js日期方法

下面列举一些常用的日期方法

/*
 * 获取日期
 * @param time
 * @returns YYYY-MM-DD格式
*/
function formateDate(time){
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();

    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    return year + '-' + month + '-' + day + '';
}

//使用
formateDate(new Date())
//输出
2022-06-23
/*
 * 获取月份
 * @param time
 * @returns YYYY-MM格式
*/
function formateMonth(time){
  let year = time.getFullYear();
  let month = time.getMonth() + 1;

  if (month < 10) {
      month = '0' + month;
  }
  return year + '-' + month;
}

//使用
formateMonth(new Date())
//输出
2022-06
/*
 * 获取上月
 * @param time
 * @returns YYYY-MM
*/
function preCurMonth(time){
  let year = time.getFullYear();
  let preMonth = time.getMonth();
  if (preMonth === 0) {
    year -= 1;
    preMonth = 12;
  }
  if(preMonth<10){
    preMonth = '0' + preMonth;
  }
  const date = year + '/' + preMonth +'/01';
  return new Date(date);
}

//使用
formateMonth(preCurMonth(new Date()))
//输出
2022-05
/*
 * 获取下月
 * @param time
 * @returns YYYY-MM
*/
function nextCurMonth(time){
  let year = time.getFullYear();
  let nextMonth = time.getMonth()+2;
  if (nextMonth === 13) {
    year += 1;
    nextMonth = 1;
  }
  if(nextMonth<10){
    nextMonth = '0' + nextMonth;
  }
  return new Date(year + '/' + nextMonth +'/01');
}

//使用
formateMonth(nextCurMonth(new Date()))
//输出
2022-07
/**
 * @param  day为-n,返回的是当前日期的前n天;day为0,返回当前日期,day为n,返回时当前日期的后n天
 * @return {*String} 返回 yyyy-MM-dd 形式
 */
function getDay(day) {
  let today = new Date();
  let targetday_milliseconds = today.getTime() + 1000 * 3600 * 24 * day;
  today.setTime(targetday_milliseconds); //注意,这行是关键代码
  let tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear + "-" + tMonth + "-" + tDate;
}

//使用
getDay(0)
getDay(-1)
getDay(1)
//输出
2022-06-23
2022-06-22
2022-06-24

```javascript
/**
 * @Descripttion: 获取传入日期的近某日 
 * @param   tempDate  传入日期
 * @param  changeDay 前n日为-n,后n日为n
 * @return {*String} 返回 yyyy-MM-dd 形式
 */
function getFormatDate(tempDate, changeDay) {
	var date = tempDate;
	var dateTime = date.getTime() + 1000 * 60 * 60 * 24 * changeDay;
	date.setTime(dateTime);
	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 formatDate = date.getFullYear() + "-" + month + "-" + strDate;
	return formatDate;
}

//使用
getFormatDate(new Date(), -2)
getFormatDate(new Date(), 0)
//输出
2022-06-21 
2022-06-23
/**
 * @Descripttion: 获取传入日期的近某月
 * @param   tempDate  传入日期
 *  @param  changeDay n月
 * @return {*String} 返回 yyyy-MM-01 形式
 */
function getFormatMonth(tempDate, count) {
	var date = tempDate;
	var month = date.getMonth() + 1;
	var year = date.getFullYear();
		
	for (var i = 0; i < count; i++) {
		month = --month;
		if (month == 0) {
		    year = --year;
		    month = 12;
		}
	}
		
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	var formatMonth = year + "-" + month + "-01";
	return formatMonth;
}

//使用
getFormatMonth(new Date(), 3)
getFormatMonth(new Date(), 1)
//输出
2022-03-01 
2022-05-01
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值