时间函数
function formatDt(date) { // ReSharper disable once IdentifierTypo var yare = date.getYear() + 1900; var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minutes = date.getMinutes(); var second = date.getSeconds(); month = month.toString().padStart(2,'0'); day = day.toString().padStart(2,'0'); hour = hour.toString().padStart(2,'0'); minutes = minutes.toString().padStart(2,'0'); second = second.toString().padStart(2,'0'); return `${yare}-${month}-${day} ${hour}:${minutes}:${second}`; }
将几天前 转换为当前时间
var dtReg = /d{4}年d{2}月d{2}日 d{2}:d{2}/ var minReg = /d+(?=分钟前)/ var houReg = /d+(?=小时前)/ var dayReg = /d+(?=天前)/ function dtToString(date) { var yare = date.getYear() + 1900 var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minutes = date.getMinutes() var second = date.getSeconds() return `${yare}-${month}-${day} ${hour}:${minutes}:${second}` } if (dtReg.test(input)) { input = input.replace('年', '-') input = input.replace('月', '-') input = input.replace('日', '') input = input+':00' } else if (minReg.test(input)) { var now = new Date() var count = Number(minReg.exec(input)) var dt = now - count * 60 * 1000 input = formatDt(new Date(dt)) } else if (houReg.test(input)) { var now = new Date() var count = Number(houReg.exec(input)) var dt = now - count * 60 * 60 * 1000 input = formatDt(new Date(dt)) } else if (dayReg.test(input)) { var now = new Date() var count = Number(dayReg.exec(input)) var dt = now - count * 60 * 60 * 24 * 1000 input = formatDt(new Date(dt)) }