获取当前日期前几天,后几天的数据
function translateDate(num) { var date1 = new Date(); var date2 = new Date(date1); date2.setDate(date1.getDate() + num); if (date2.getMonth() < 9) { if (date2.getDate() < 10) { var time2 = date2.getFullYear() + "-0" + (date2.getMonth() + 1) + "-0" + date2.getDate(); } else { var time2 = date2.getFullYear() + "-0" + (date2.getMonth() + 1) + "-" + date2.getDate(); } } else { if (date2.getDate() < 10) { var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-0" + date2.getDate(); } else { var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate(); } } return time2; }
num是正数表示之后的时间,num负数表示之前的时间,0表示今天
请求返回秒,转换为具体时间
translateTime(time) { let t = new Date(time * 1000); let year = t.getFullYear(); let month = t.getMonth() + 1; if (month < 10) { month = "0" + month; } let date = t.getDate(); if (date < 10) { date = "0" + date; } let hour = t.getHours(); if (hour < 10) { hour = "0" + hour; } let minute = t.getMinutes(); if (minute < 10) { minute = "0" + minute; } let second = t.getSeconds(); if (second < 10) { second = "0" + second; } return ( year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second ); }
获取时间值的区别
new Date('2020-07-09').getTime() //获取到的是该日期八点的时间戳 new Date().getTime() //获取到的是当前日期的时间戳,精确到毫秒