可能后面会用上,下面是时间戳转换为日期
function getTimeStr(time){ var now = new Date(time*1000); var year=now.getFullYear(); var month=now.getMonth()+1; if(month < 10){ month = '0' + month; } var date=now.getDate(); if(date < 10){ date = '0' + date; } var hour=now.getHours(); if(hour < 10){ hour = '0' + hour; } var minute=now.getMinutes(); if(minute < 10){ minute = '0' + minute; } var second=now.getSeconds(); if(second < 10){ second = '0' + second; } return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; }
格式是“2018-09-04 14:26:20”
下面我们看看日期转为时间戳的方法
function formatNumber (n) { n = n.toString() return n[1] ? n : '0' + n; } // 参数number为毫秒时间戳,format为需要转换成的日期格式 function formatTime (number, format) { let time = new Date(number) let newArr = [] let formatArr = ['Y', 'M', 'D', 'h', 'm', 's'] newArr.push(time.getFullYear()) newArr.push(formatNumber(time.getMonth() + 1)) newArr.push(formatNumber(time.getDate())) newArr.push(formatNumber(time.getHours())) newArr.push(formatNumber(time.getMinutes())) newArr.push(formatNumber(time.getSeconds())) for (let i in newArr) { format = format.replace(formatArr[i], newArr[i]) } return format; }
传入格式是
formatTime(1545903266795, "Y-M-D h:m:s")