有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。


当前回答

var date_today=new Date();
var formated_date = formatDate(date_today);//Calling formatDate Function

var input_date="2015/04/22 11:12 AM";

var currentDateTime = new Date(Date.parse(formated_date));
var inputDateTime   = new Date(Date.parse(input_date));

if (inputDateTime <= currentDateTime){
    //Do something...
}

function formatDate(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'PM' : 'AM';

    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    hours   = hours < 10 ? '0'+hours : hours ;

    minutes = minutes < 10 ? '0'+minutes : minutes;

    var strTime = hours+":"+minutes+ ' ' + ampm;
    return  date.getFullYear()+ "/" + ((date.getMonth()+1) < 10 ? "0"+(date.getMonth()+1) :
    (date.getMonth()+1) ) + "/" + (date.getDate() < 10 ? "0"+date.getDate() :
    date.getDate()) + " " + strTime;
}

其他回答

简单的方法是,

var first = '2012-11-21';
var second = '2012-11-03';

if (new Date(first) > new Date(second) {
    .....
}

我对这个问题的简单回答

checkDisabled(date) {
    const today = new Date()
    const newDate = new Date(date._d)
    if (today.getTime() > newDate.getTime()) {
        return true
    }
    return false
}
       

您可以使用以下代码:

var firstValue = "2012-05-12".split('-');
var secondValue = "2014-07-12".split('-');

 var firstDate=new Date();
 firstDate.setFullYear(firstValue[0],(firstValue[1] - 1 ),firstValue[2]);

 var secondDate=new Date();
 secondDate.setFullYear(secondValue[0],(secondValue[1] - 1 ),secondValue[2]);     

  if (firstDate > secondDate)
  {
   alert("First Date  is greater than Second Date");
  }
 else
  {
    alert("Second Date  is greater than First Date");
  }

还要检查Date类的MDN文章。

function compare_date(date1, date2){
const x = new Date(date1)
const y = new Date(date2)
function checkyear(x, y){
    if(x.getFullYear()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getFullYear()<y.getFullYear()){
        return "Date2 > Date1"
    }
    else{
        return checkmonth(x, y)
    }
}
function checkmonth(x, y){
    if(x.getMonth()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getMonth()<y.getMonth){
        return "Date2 > Date1"
    }
    else {
        return checkDate(x, y)
    }
}
function checkDate(x, y){
    if(x.getDate()>y.getFullYear()){
        return "Date1 > Date2"
    }
    else if(x.getDate()<y.getDate()){
        return "Date2 > Date1"
    }
    else {
        return checkhour(x,y)
    }
}
function checkhour(x, y){
    if(x.getHours()>y.getHours()){
        return "Date1 > Date2"
    }
    else if(x.getHours()<y.getHours()){
        return "Date2 > Date1"
    }
    else {
        return checkhmin(x,y)
    }
}
function checkhmin(x,y){
    if(x.getMinutes()>y.getMinutes()){
        return "Date1 > Date2"
    }
    else if(x.getMinutes()<y.getMinutes()){
        return "Date2 > Date1"
    }
    else {
        return "Date1 = Date2"
    }
}
return checkyear(x, y)

减去两个日期,得到以毫秒为单位的差值,如果得到0,则是相同的日期

function areSameDate(d1, d2){
    return d1 - d2 === 0
}