欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-30 09:03:32
...

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)];
    },