JS時間選擇(前n天,當前,後n天)
阿新 • • 發佈:2018-12-14
在專案中,有時候需要查詢一段時間以內的訂單。為了方便使用者快速選擇時間區域,並寫了這個需求,格式
為:YY-MM-DD hh:mm:ss
function getBeforeDate(n){//n為你要傳入的引數,當前為0,前一天為-1,後一天為1
var date = new Date() ;
var year,month,day ;
date.setDate(date.getDate()+n);
year = date.getFullYear();
month = date.getMonth()+1;
day = date.getDate();
var strHours = date.getHours();
var strMinutes = date.getMinutes();
var strSeconds = date.getSeconds();
s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day)+' '+ ( strHours <10 ? ('0'+strHours ) : strHours)+':'+(strMinutes < 10 ? ('0'+strMinutes) : strMinutes)+':'+(strSeconds < 10 ? ('0'+strSeconds) : strSeconds);
return s
}