1. 程式人生 > 其它 >layui 獲取時間和日期(前些天,後些天,當前)

layui 獲取時間和日期(前些天,後些天,當前)

第一:獲取當前時間

var now = new Date();     var year = now.getFullYear();//獲取當前年份     var month = now.getMonth()+1;//當前月份     if(month<10)       month = "0" + month;     else{       month = month;     }     var day = now.getDate();//當前天份     if(day<10)       day = "0" + day;     else{       day = day;     }     var time=now.getHours();//當前小時
    var getMinutes=now.getMinutes();//當前分鐘     var getSeconds=now.getSeconds();//當前秒鐘     var time=year+"-"+month+"-"+day+" "+time+":"+getMinutes+":"+getSeconds; //獲取當前時分秒     var datatime=year+"-"+month+"-"+day; //獲取當前年月日 第二:獲取前些天日期    var now = new Date().getTime(); //獲取毫秒數     var before = new Date(now - 2 * 86400 * 1000); //這裡的2,表示2天前,例如:需要當前日期的10天前,只需要將2改成10就可以了
    var beforeyear = before.getFullYear();     var beforemonth = before.getMonth()+1;     if(beforemonth<10)     beforemonth = "0" + beforemonth;     else{       beforemonth = beforemonth;     }     var beforedate = before.getDate();//當前天份     if(beforedate<10)     beforedate = "0" + beforedate;     else{
      beforedate = beforedate;     }         var datatimeOne = beforeyear +"-" + beforemonth + "-" + beforedate; 第三:獲取後些天的日期(和前些天一樣,只需要把)  var now = new Date().getTime(); //獲取毫秒數     var before = new Date(now + 2 * 86400 * 1000); //這裡的2,表示2天后,例如:需要當前日期的10天后,只需要將2改成10就可以了     var beforeyear = before.getFullYear();     var beforemonth = before.getMonth()+1;     if(beforemonth<10)     beforemonth = "0" + beforemonth;     else{       beforemonth = beforemonth;     }     var beforedate = before.getDate();//當前天份     if(beforedate<10)     beforedate = "0" + beforedate;     else{       beforedate = beforedate;     }         var datatimeOne = beforeyear +"-" + beforemonth + "-" + beforedate;