vue更改年月日的日期时间(vue2)

<template>
  <div>{{  nowDate }}</div>
</template>
<script>
export default {
  data() {
    return {
      nowDate: "", // 当前日期
      year: "",
      month: "",
      day: ""
    };
  },
  methods: {
    // 正常的是月份+1
    formatDate() {
      let date = new Date();
      this.month = date.getMonth()+1; //月
      this.day = date.getDate();//日
      /*
     /^\d$/.test(date.getDate())
        ? "0" + date.getDate()
        : date.getDate();
      这个是不满10前面自动补0 
    */
      this.year = date.getFullYear(); //年
      // 这个是更改月份 当天数小于3号时 月份-2 PS正常是加1 不加为-1 
      // 反则月份-1显示
      // if (this.day < 3) {
      //  this.month = (date.setMonth(date.getMonth() - 1)) ;//这个是改变日期的变化
      //  this.month=date.getMonth()
      // } else {
      //   this.month = date.getMonth();
      // }
      this.nowDate =`${this.year}/${this.month}/${this.day}`;
   
    }
  },
  mounted() {
    this.formatDate();
  }
};
</script>

不满10前面自动补0

  formatTime() {
      let date = new Date();
      // this.month = date.getMonth(); //月
      this.month = /^\d$/.test(date.getMonth())
        ? "0" + date.getMonth()
        : date.getMonth(); //月
      // this.day = date.getDate();
      this.day = /^\d$/.test(date.getDate())
        ? "0" + date.getDate()
        : date.getDate();
      this.year = date.getFullYear(); //年
      if (this.day < 3) {
        this.month = date.setMonth(date.getMonth() - 1);
        this.month = /^\d$/.test(date.getMonth())
          ? "0" + date.getMonth()
          : date.getMonth();
      } else {
        this.month = /^\d$/.test(date.getMonth())
          ? "0" + date.getMonth()
          : date.getMonth();
      }
      this.nowdata = Number(`${this.year}${this.month}${this.day}`);
    },

这个是手控的时间日期的格式

Vue 获取当下日期时间及周几

https://www.jianshu.com/p/023623d0fa6a

判断如果周2当前日期-2 否则正常

!vue!<template>
  <div>
    <p>{{nowWeek}}</p>
    <p>
      <span>{{nowDate}}</span>
    </p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nowWeek: "",
      nowDate: "",
    };
  },
  mounted() {
    this.setNowTimes();
  },
  methods:{
    setNowTimes () {  
        let myDate = new Date()  
        let wk = myDate.getDay()  
        let yy = String(myDate.getFullYear())  
        let mm = myDate.getMonth() + 1  
        let dd = String(myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate())  
        let hou = String(myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours())  
        let min = String(myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes())  
        let sec = String(myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds())  
        let weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
        let week = weeks[wk]  
        if(week ==='星期二'){
          dd = myDate.setDate(myDate.getDate() - 2);
         dd= /^\d$/.test(myDate.getDate())
          ? "0" + myDate.getDate()
          :myDate.getDate();
        }else{
           dd = /^\d$/.test(myDate.getDate())
          ? "0" +myDate.getDate()
          : myDate.getDate();
      }
        this.nowDate = yy + '-' + mm + '-' + dd  
        this.nowWeek = week
}
}
};
</script>
<style>
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值