JS設定前後7 30天(前n天,當前,後n天)

2021-08-28 19:18:13 字數 747 閱讀 7297

在專案中,有時候需要查詢一段時間以內的訂單。為了方便使用者快速選擇時間區域,並寫了這個需求,格式

為: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

判斷當前日期前N天或後N天

話不多說直接上 num為過去或未來日子的標準 例 30 為當前日期前30天,30 為當前日期後30天 function beforedate num,dateparameter else newdate new date newdate 如果月份長度少於2,則前加 0 補位 if newdate.g...

js N秒前,N分鐘前,N小時前,N天前,N月前

注意 我對原方法做了稍微改動,之前引數是時間戳,我改成了時間 然後在下面又轉換成了時間戳,因為我資料庫返回的就是時間 timeago datetime var minc diffvalue minute 計算時間差的分,時,天,周,月 var hourc diffvalue hour var day...

python獲取N天前 後日期

獲取指定日期前後n天日期 方法 字串轉化為日期格式後,進行加減,然後再轉化為字串 import datetime from datetime import timedelta 字串轉化為日期格式 target date datetime.datetime.strptime 2019 03 26 y ...