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