Vue项目时间过滤器

Vue项目时间过滤器

//获取上月年月
export function getLastMonth(val) {
   var timeStamp = val ? new Date(val) : new Date(),
      year, month;
   year = timeStamp.getFullYear()
   month = timeStamp.getMonth() + 1
   month = month == 1 ? 12 : month - 1 //上月数据
   month == 12 && (year -= 1) //上一年条件
   month = month < 10 ? '0' + month : month //mm格式,ie兼容
   return year + '-' + month //'-'兼容ie
}

//获取下月年月
export function getNextMonth(val) {
   var timeStamp = val ? new Date(val) : new Date(),
      year, month;
   year = timeStamp.getFullYear()
   month = timeStamp.getMonth() + 1
   month = month == 12 ? 1 : month + 1 //上月数据
   month == 1 && (year += 1) //上一年条件
   month = month < 10 ? '0' + month : month //mm格式,ie兼容
   return year + '-' + month //'-'兼容ie
}

//获取当前年月日
export function getTargetDay(val) {
   var nowDate = val ? new Date(val) : new Date();
   var year = nowDate.getFullYear();
   var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
      : nowDate.getMonth() + 1;
   var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
      .getDate();
   var dateStr = year + "-" + month + "-" + day;
   return dateStr
}

// 获取当前年月
export function getTargetMonth(val) {
   var nowDate = val ? new Date(val) : new Date();
   var year = nowDate.getFullYear();
   var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
      : nowDate.getMonth() + 1;
   var dateStr = year + "-" + month;
   return dateStr
}

// 获取当前月日
export function getTargetDate(val) {
   var nowDate = val ? new Date(val) : new Date();
   var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
      : nowDate.getMonth() + 1;
   var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
      .getDate();
   var dateStr = month + "-" + day;
   return dateStr
}

//获取当前年月日时分秒
export function getTargetTime(val) {
   var nowDate = val ? new Date(val) : new Date();
   var year = nowDate.getFullYear();
   var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
      : nowDate.getMonth() + 1;
   var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
      .getDate();
   var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate
      .getHours();
   var mint = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate
      .getMinutes();
   var sec = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate
      .getSeconds();
   var dateStr = year + "-" + month + "-" + day + " " + hour + ":" + mint + ":" + sec;
   return dateStr
}

//获取当前时分秒
export function getTargetHms(val) {
   var nowDate = val ? new Date(val) : new Date();
   var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate
      .getHours();
   var mint = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate
      .getMinutes();
   var sec = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate
      .getSeconds();
   var dateStr = hour + ":" + mint + ":" + sec;
   return dateStr
}
// 获取当前时分
export function getTargetHm(val) {
   var nowDate = val ? new Date(val) : new Date();
   var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate
      .getHours();
   var mint = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate
      .getMinutes();
   var dateStr = hour + ":" + mint;
   return dateStr
}

//获取昨日
export function getYesterday(val) {
   var day1 = val ? new Date(val) : new Date();
   day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
   var month = (day1.getMonth() + 1) > 9 ? day1.getMonth() + 1 : '0' + (day1.getMonth() + 1)
   var day = day1.getDate() > 9 ? day1.getDate() : '0' + day1.getDate()
   var s1 = day1.getFullYear() + "-" + month + "-" + day;
   return s1
}

//获取明日
export function getTormmoro(val) {
   var day3 = val ? new Date(val) : new Date();
   day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
   var month = (day3.getMonth() + 1) > 9 ? day3.getMonth() + 1 : '0' + (day3.getMonth() + 1)
   var day = day3.getDate() > 9 ? day3.getDate() : '0' + day3.getDate()
   var s3 = day3.getFullYear() + "-" + month + "-" + day;
   return s3
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值