vue日期模板

**

vue的日期模板(今天,明天,本周(从周一到当天),本月,上月)

**

<script>
export default {
    data(){
        return{
             now :new Date(),
        }
    },
    methods: {
         //时间筛选今天
    times(){
           this.seleDate=[this.now.getFullYear()+'-'+(this.now.getMonth()+1)+'-'+this.now.getDate(),this.now.getFullYear()+'-'+(this.now.getMonth()+1)+'-'+this.now.getDate()]
           return this.seleDate;
    },
  //上月
    lastmonths(){
        var d = new Date(this.now.getFullYear(), (this.now.getMonth()), 0);
        this.seleDate=[this.now.getFullYear()+'-'+this.now.getMonth()+'-'+1,this.now.getFullYear()+'-'+this.now.getMonth()+'-'+d.getDate()]
        return this.seleDate;
    },
    //本月
    thismonths(){
       this.seleDate=[this.now.getFullYear()+'-'+(this.now.getMonth()+1)+'-'+1,this.now.getFullYear()+'-'+(this.now.getMonth()+1)+'-'+this.now.getDate()]
        return this.seleDate;
    },
    
//本周
thisweeks(){
var now = new Date(); // 当前日期
        var nowDayOfWeek = now.getDay(); // 今天本周的第几天
        var nowDay = now.getDate(); // 当前日
        var nowMonth = now.getMonth(); // 当前月
        var nowYear = now.getYear(); // 当前年
        nowYear += (nowYear < 2000) ? 1900 : 0;
        var day = nowDayOfWeek || 7;
},
//格式日期
 formatDate(date) {
        var myyear = date.getFullYear();
        var mymonth = date.getMonth() + 1;
        var myweekday = date.getDate();
  
        if (mymonth < 10) {
            mymonth = "0" + mymonth;
        }
        if (myweekday < 10) {
            myweekday = "0" + myweekday;
        }
        return (myyear + "-" + mymonth + "-" + myweekday);
    },
    
   //上周、本周、下周
 //获取上周起始时间结束时间、下周起始时间结束时间开始时间和本周起始时间结束时间;(西方)
function getTime(n) {
  var now = new Date();
  var year = now.getFullYear();
  //因为月份是从0开始的,所以获取这个月的月份数要加1才行
  var month = now.getMonth() + 1;
  var date = now.getDate();
  var day = now.getDay();
  console.log(date);
  //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
  if (day !== 0) {
    n = n + (day - 1);
  } else {
    n = n + day;
  }
  if (day) {
    //这个判断是为了解决跨年的问题
    if (month > 1) {
      month = month;
    }
    //这个判断是为了解决跨年的问题,月份是从0开始的
    else {
      year = year - 1;
      month = 12;
    }
  }
  now.setDate(now.getDate() - n);
  year = now.getFullYear();
  month = now.getMonth() + 1;
  date = now.getDate();
  // console.log(n);
  var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
  return s;
}

/***参数都是以周一为基准的***/
//上周的开始时间
// console.log(getTime(7));
//上周的结束时间
// console.log(getTime(1));
//本周的开始时间
// console.log(getTime(0));
//本周的结束时间
// console.log(getTime(-6));
//下周的开始时间
// console.log(getTime(-7));
//下周结束时间
// console.log(getTime(-13));

    //昨天
    yesterdays(){
       var day,month;
       if((this.now.getDate()-1)<=0){
           var d = new Date(this.now.getFullYear(), (this.now.getMonth()), 0);
           day=d.getDate();
           month=this.now.getMonth()
       }else{
           day=this.now.getDate()-1;
            month=this.now.getMonth()+1
       }
     this.seleDate=[this.now.getFullYear()+'-'+month+'-'+day,this.now.getFullYear()+'-'+month+'-'+day]
      return this.seleDate;
    },
    //周(传0当天)
     lowerweeks(n){
  var now = new Date();
  var year = now.getFullYear();
  //因为月份是从0开始的,所以获取这个月的月份数要加1才行
  var month = now.getMonth() + 1;
  var date = now.getDate();
  var day = now.getDay();
  //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
  if (day !== 0) {
    n = n + (day - 1);
  } else {
    n = n + day;
  }
  if (day) {
    //这个判断是为了解决跨年的问题
    if (month > 1) {
      month = month;
    }
    //这个判断是为了解决跨年的问题,月份是从0开始的
    else {
      year = year - 1;
      month = 12;
    }
  }
  now.setDate(now.getDate() - n);
  year = now.getFullYear();
  month = now.getMonth() + 1;
  date = now.getDate();
  // console.log(n);
  var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
  return s;
    }
    console.log(s)//  年-月-日  xxxx-xx-xx
    //
   },


//月data为(xxxx-xx-xx)  上月传-1  下月传1
function  months(date,data){
  let timeday='01';
  var arr = date.split('-');  //用-将获取到的日期分割
        var year = arr[0];   //获取当前日期的年份  
        var month = arr[1];  //获取当前日期的月份  
        var day = arr[2];    //获取当前日期的日  
        var month2 = parseInt(month) + data;  //获取上个月的月份
        
        if (month2 == 0) {  
            year = parseInt(year) + data;  //获取上个月的年份
            month2 = 12;  
        }else if(month2==13){
          year = parseInt(year) + data;  //获取下个月的年份
          month2 = 1;  
        }  
        if(month2==2&&year%4==0){
          timeday='29'
         }else if(month2==2&&year%4!=0){
          timeday='28'
         }else if(month2==1|month2==3|month2==5|month2==7|month2==8|month2==10|month2==12){
          timeday='31'
         }else if(month2==4|month2==6|month2==11){
          timeday='30'
         }
        var days2 = new Date(year, month2, 0);  //获取当前日期中月的天数
        days2 = days2.getDate();  //天数
        if (day > days2) {  
            day = days2;  
        }  
        if (month2 < 10) {  //如果月份少于10  则月份格式为 01 02 .. 09 10 11 12
            month2 = '0' + month2;  
        }  
        var NewsDate = [year + '-' + month2 + '-' + '01',year + '-' + month2 + '-' + timeday];  //将新的年月日用-拼接起来
        return NewsDate;  
};
}
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值