中国标准时间、时间戳、年月日等日期格式处理

1.new Date( ) 输出
Fri Mar 06 2020 15:18:29 GMT+0800 (中国标准时间)

2.date.getTime() 得到时间戳 输出
1583479672091

3.date.setTime(); 设置时间 (时间戳)

4.date.getDate()、date.getMonth() 输出
年月日

5.将中国标准时间转化未年月日(2020-03-13)

  getTime(time) {
      var date = new Date(time);
      var day = ("0" + date.getDate()).slice(-2);
      //格式化月,如果小于9,前面补0
      var month = ("0" + (date.getMonth() + 1)).slice(-2);
      //拼装完整日期格式
      var today = date.getFullYear() + "-" + month + "-" + day;
      return today;
    },
    

5.将中国标准时间转化为两个相隔一周的时间

getThirtyTime() {
  const end = new Date();
  const start = new Date();
  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
  this.searchForm.orderTime = [this.getTime(start), this.getTime(end)];
},

重要例子如下

 <el-form-item label="时段:">
              <el-button type="primary" @click="today" plain>今天</el-button>
              <el-button type="primary" @click="yestoday" plain>昨天</el-button>
              <el-button type="primary" @click="weekly" plain>近7天</el-button>
              <el-button type="primary" @click="month" plain>近30天</el-button>
            </el-form-item>
 today() {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime());
      this.searchForm.orderTime = [this.getTime(start), this.getTime(end)];
    },
    yestoday() {
      //const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
      this.searchForm.orderTime = [this.getTime(start), this.getTime(start)];
    },
    weekly(){
      const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          this.searchForm.orderTime = [this.getTime(start), this.getTime(end)];
    },
    month(){
       const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
         this.searchForm.orderTime = [this.getTime(start), this.getTime(end)];
    },
  getTime(time) {
      var date = new Date(time);
      var day = ("0" + date.getDate()).slice(-2);
      //格式化月,如果小于9,前面补0
      var month = ("0" + (date.getMonth() + 1)).slice(-2);
      //拼装完整日期格式
      var today = date.getFullYear() + "-" + month + "-" + day;
      return today;
    },
    getThirtyTime() {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      this.searchForm.orderTime = [this.getTime(start), this.getTime(end)];
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值