前端开发中,常遇到的时间转换方法。
moment: {
getMonth: function (strDate) {
if (!strDate) return ''
return Number(strDate.substring(4, 6))
},
getYear: function (strDate) {
if (!strDate) return ''
return Number(strDate.substring(0, 4))
},
getDate: function (strDate) {
if (!strDate) return ''
return Number(strDate.substring(6))
},
// 获得某月的天数
getMonthDays: function (year, month) {
if (!year || !month) {
return 0
}
let monthStartDate = new Date(year, month - 1, 1)
let monthEndDate = new Date(year, month, 1)
return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
},
/* 获取前一天 */
getLastDay: function (strDate) {
if (!strDate) return ''
let tempDate = new Date(this.getYear(strDate), this.getMonth(strDate) - 1, this.getDate(strDate) - 1)
return this.date2Str(tempDate)
},
/* 获取后一天 */
getNextDay: function (strDate) {
if (!strDate) return ''
let tempDate = new Date(this.getYear(strDate), this.getMonth(strDate) - 1, this.getDate(strDate) + 1)
return this.date2Str(tempDate)
},
/* 获取两个时间段的天数,endTime为空则为当前时间 */
getBetweenDays: function (startTime, endTime) {
if (!startTime || !endTime) {
return ''
}
let monthStartDate = Date.UTC(this.getYear(startTime), this.getMonth(startTime) - 1, this.getDate(startTime))
let monthEndDate = Date.UTC(this.getYear(endTime), this.getMonth(endTime) - 1, this.getDate(endTime))
return Math.ceil((monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24))
},
/* 前一月 */
getPreviousMonth: function (strTime, format) {
if (!strTime) return ''
let year = this.getYear(strTime)
let month = this.getMonth(strTime)
let day = strTime.substring(6)
if (month - 1 === 0) {
year--
month = 12
} else {
month--
}
month = month > 9 ? month : '0' + month
let result = year + '' + month + '' + day
if (format) {
result = this._format(format, year, month, day)
}
return result
},
_format: function (format, year, month, day) {
return format.replace('YYYY', year).replace('YY', (year + '').substr(-2)).replace('MM', month).replace('M', Number(month)).replace('DD', day).replace('D', Number(day))
},
/* 下一月 */
getNextMonth: function (strTime, format) {
if (!strTime) return ''
let year = this.getYear(strTime)
let month = this.getMonth(strTime)
let day = strTime.substring(6)
month++
if (month === 13) {
year++
month = 1
}
month = month > 9 ? month : '0' + month
let result = year + '' + month + '' + day
if (format) {
result = this._format(format, year, month, day)
}
return result
},
/* 加多少个月 */
addMonth: function (strTime, months, format) {
if (!strTime) return ''
let tempDate = new Date(this.transformDate(strTime, 'YYYY/MM/DD'))
tempDate.setMonth(tempDate.getMonth() + Number(months))
if (!format) {
return this.date2Str(tempDate)
} else {
let month = tempDate.getMonth() + 1
month = month > 9 ? month : '0' + month
let date = tempDate.getDate()
date = date > 9 ? date : '0' + date
return this._format(format, tempDate.getFullYear(), month, date)
}
},
/* 日期格式 */
transformDate: function (strTime, format) {
if (!strTime || !format) return ''
let year = strTime.substring(0, 4)
let month = strTime.substring(4, 6)
let day = strTime.substring(6)
return this._format(format, year, month, day)
},
/* 获取当前的年月日==>20161130 */
getCurrDate: function () {
return this.date2Str(new Date())
},
/* 日期转字符串:==>20161130 */
date2Str: function (dateInfo) {
if (!dateInfo) return ''
if (dateInfo instanceof Date) {
let month = dateInfo.getMonth() + 1
month = month > 9 ? month : '0' + month
let date = dateInfo.getDate()
date = date > 9 ? date : '0' + date
return dateInfo.getFullYear() + '' + month + date
}
},
/* 格式化时间 YYYY-MM-DD hh:mm:ss */
formatTime: function (timestamp, format) {
let date = new Date(timestamp)
let year = date.getFullYear()
let month = date.getMonth() + 1
month = month > 9 ? month : '0' + month
let day = date.getDate()
day = day > 9 ? day : '0' + day
let hours = date.getHours()
hours = hours > 9 ? hours : '0' + hours
let minutes = date.getMinutes()
minutes = minutes > 9 ? minutes : '0' + minutes
let seconds = date.getSeconds()
seconds = seconds > 9 ? seconds : '0' + seconds
let result = this._format(format, year, month, day)
return result.replace('hh', hours).replace('mm', minutes).replace('ss', seconds)
}
},