美文网首页
格式化时间

格式化时间

作者: asmuzi | 来源:发表于2019-01-18 15:51 被阅读0次
        getTime() {
          let now = new Date();
          console.log(now.getHours());
          
          let startDate = new Date(
            Date.UTC(
              now.getFullYear(),
              now.getMonth(),
              now.getDate(),
              now.getHours(),
              now.getMinutes()
            )
          )
            .toISOString()
            .slice(0, 10);
          let endDate = new Date(
            Date.UTC(
              now.getFullYear(),
              now.getMonth(),
              now.getDate() + 1,
              now.getHours(),
              now.getMinutes()
            )
          )
            .toISOString()
            .slice(0, 10);
          this.ruleForm.examDate.push(startDate);
          this.ruleForm.examDate.push(endDate);
          console.log(this.ruleForm.examDate);
          
        }
    
    
        getTime() {
          let now = new Date();
          let startDate = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate(),
            now.getHours(),
          );
          let endDate = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate() + 1,
            now.getHours(),
          );
          this.ruleForm.examDate.push(startDate);
          this.ruleForm.examDate.push(endDate);
          console.log(this.ruleForm.examDate);
        },
    
        getTime() {
          let startDate = this.formatDate(new Date(), "yyyy-MM-dd hh:mm:00");
          let endDate = this.formatDate(new Date(), "yyyy-MM-dd hh:mm:00");
          this.ruleForm.examDate.push(startDate);
          this.ruleForm.examDate.push(endDate);
          console.log(this.ruleForm.examDate);
        },
        formatDate(date, fmt) {
          if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(
              RegExp.$1,
              (date.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
          }
          let o = {
            "y+": date.getFullYear(),
            "M+": date.getMonth() + 1,
            "d+": date.getDate(),
            "h+": date.getHours(),
            "m+": date.getMinutes(),
            "s+": date.getSeconds()
          };
          for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
              let str = o[k] + "";
              fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? str : this.padLeftZero(str)
              );
            }
          }
          return fmt;
        },
        padLeftZero(str) {
          return ("00" + str).substr(str.length);
        },
    

    相关文章

      网友评论

          本文标题:格式化时间

          本文链接:https://www.haomeiwen.com/subject/ezmndqtx.html