我想把一个标准时间转换为2019-01-01 09:01这样的格式,当月份和时分秒小于10的时候前面补上0,于是用了上面的三元来判断,一个是开始时间,一个是结束时间,但是写完发现好臃肿,请问一下,这样的话如何优化使得代码更精简?

splitDate() {
      const createStartTime = this.orderDate[0]
      const createEndTime = this.orderDate[1]
      const createStartMonth = (createStartTime.getMonth() + 1) < 10 ? '0' + (createStartTime.getMonth() + 1) : createStartTime.getMonth() + 1
      const createEndMonth = (createEndTime.getMonth() + 1) < 10 ? '0' + (createEndTime.getMonth() + 1) : createEndTime.getMonth() + 1
      const createStartDate = createStartTime.getDate() < 10 ? '0' + (createStartTime.getDate()) : createStartTime.getDate()
      const createEndDate = createEndTime.getDate() < 10 ? '0' + (createEndTime.getDate()) : createEndTime.getDate()
      const createStartHours = createStartTime.getHours() < 10 ? '0' + createStartTime.getHours() : createStartTime.getHours()
      const createEndHours = createEndTime.getHours() < 10 ? '0' + createEndTime.getHours(): createEndTime.getHours()
      const createStartMinutes = createStartTime.getMinutes() < 10 ? '0' + createStartTime.getMinutes() : createStartTime.getMinutes()
      const createEndMinutes = createEndTime.getMinutes() < 10 ? '0' + createEndTime.getMinutes(): createEndTime.getMinutes()
      const createStartSeconds = createStartTime.getSeconds() < 10 ? '0' + createStartTime.getSeconds() : createStartTime.getSeconds()
      const createEndSeconds = createEndTime.getSeconds() < 10 ? '0' + createEndTime.getSeconds(): createEndTime.getSeconds()
      this.orderForm.createStartTime = createStartTime.getFullYear() + '-' + createStartMonth + '-' + createStartDate + ' ' + createStartHours + ':' + createStartMinutes + ':' + createStartSeconds
      this.orderForm.createEndTime = createEndTime.getFullYear() + '-' + createEndMonth + '-' + createEndDate + ' ' + createEndHours + ':' + createEndMinutes + ':' + createEndSeconds
    }

···回复:

Date.prototype.toFormattedString = function (format) {
    const O = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "H+": this.getHours(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "f": this.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in O) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (O[k]) : (("00" + O[k]).substr(("" + O[k]).length)));
        }
    }
    return format;
};

各种格式,都能胜任:

let date = new Date();

date.toFormattedString('yyyy-MM-dd HH:mm:ss'); // 2019-11-07 08:52:13
date.toFormattedString('yy/MM/dd'); // 19/01/01
date.toFormattedString('M月d日'); // 11月7日
date.toFormattedString('HH:mm:ss'); // 08:52:13
date.toFormattedString('m分,s秒'); // 52分,13秒

···回复:

个人建议可以用插件来做。dayjs moment都可以处理。个人推荐dayjs,dayjs所占的内存比moment小

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Scroll Up