函數
getDateXHL(num) { var date = new Date(); var dateNumber = date.getTime(); var differ = num * 24 * 60 * 60 * 1000; var needDateNumber = new Date(dateNumber + differ); var year = needDateNumber.getFullYear(); var month = needDateNumber.getMonth() + 1; var day = needDateNumber.getDate(); var monthX = month < 10 ? "0" + month : month; var dayX = day < 10 ? "0" + day : day; var nowDate = year + "-" + monthX + "-" + dayX; return nowDate; }
調用
//獲取7天之前日期 var aaa = getDateXHL(-7); //獲取當前日期 var bbb = getDateXHL(0); //獲取30天之后日期 var ccc = getDateXHL(30);
根據時間戳轉換時間
//如果時間戳為13位,可以直接轉換 var a = 1494806400000; //如果為10位,需要先乘1000再轉換 var a = 1494806400 * 1000; var time = new Date(a);
