天天看點

js比較日期 - JavaScript

function checkDa(){
  var myDate = new Date();
  var myYear = myDate.getYear();
  var myMonth = myDate.getMonth();
  var myDay = myDate.getDate();
  var mdate = myYear + "-"
      + (myMonth < 9 ? ("0" + (myMonth + 1)) : (myMonth + 1)) + "-"
      + (myDay < 10 ? ("0" + myDay) : myDay);
  var EffectiveDate = fm.EffectiveDate.value;
  var DeadLine = fm.DeadLine.value;
  if (EffectiveDate < mdate) {
    alert("生效日期需大于等于目前系統日期,請重新輸入!");
    return false;
  }
  return true;
}      
// 其中year_res為2020, month_res為08 
var yearMonthNew = new Date(year_res+"/"+month_res+"/"+"01");
// setMonth: 設定新的年份,  其中getMonth擷取月份, +2 并增加兩個月
yearMonthNew.setMonth(yearMonthNew.getMonth() + 2);

// 如果比較日期, 可以直接 > < = 來進行判斷