时间格式

frontOneHour(fmt) {
      var currentTime = new Date(new Date().getTime());
      // console.log(currentTime); // Wed Jun 20 2018 16:12:12 GMT+0800 (中国标准时间)
      var o = {
        "M+": currentTime.getMonth() + 1, // 月份
        "d+": currentTime.getDate(), //
        "h+": currentTime.getHours(), // 小时
        "m+": currentTime.getMinutes(), //
        "s+": currentTime.getSeconds(), //
        "q+": Math.floor((currentTime.getMonth() + 3) / 3), // 季度
        S: currentTime.getMilliseconds() // 毫秒
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          (currentTime.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
      }
      return fmt;
    },

调用方法:

frontOneHour("yyyy年MM月dd日 hh:mm:ss");
frontOneHour("yyyy/MM/dd hh:mm:ss");
frontOneHour("yyyy-MM-dd- hh:mm:ss");
原文地址:https://www.cnblogs.com/zhanghailing/p/13667405.html