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


当前回答

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)

其他回答

var date = new Date(); // will give you todays date.

// following calls, will let you set new dates.
setDate()   
setFullYear()   
setHours()  
setMilliseconds()   
setMinutes()    
setMonth()  
setSeconds()    
setTime()

var yesterday = new Date();
yesterday.setDate(...date info here);

if(date>yesterday)  // will compare dates

您可以使用以下代码:

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文章。

简短的回答

下面是一个函数,如果from dateTime>to dateTime Demo在操作中,则返回{boolean}

var from = '08/19/2013 00:00'
var to = '08/12/2013 00:00 '

function isFromBiggerThanTo(dtmfrom, dtmto){
   return new Date(dtmfrom).getTime() >=  new Date(dtmto).getTime() ;
}
console.log(isFromBiggerThanTo(from, to)); //true

解释

jsFiddle公司

var date_one = '2013-07-29 01:50:00',
date_two = '2013-07-29 02:50:00';
//getTime() returns the number of milliseconds since 01.01.1970.
var timeStamp_date_one = new Date(date_one).getTime() ; //1375077000000 
console.log(typeof timeStamp_date_one);//number 
var timeStamp_date_two = new Date(date_two).getTime() ;//1375080600000 
console.log(typeof timeStamp_date_two);//number 

因为您现在的日期时间都是数字类型您可以将它们与任何比较操作进行比较

(>,<,=,!=,==,!==,>=和<=)

Then

如果您熟悉C#自定义日期和时间格式字符串,这个库应该做完全相同的事情,并帮助您格式化日期和时间dtmFRM,无论您传递的是日期时间字符串还是unix格式

用法

var myDateTime = new dtmFRM();

alert(myDateTime.ToString(1375077000000, "MM/dd/yyyy hh:mm:ss ampm"));
//07/29/2013 01:50:00 AM

alert(myDateTime.ToString(1375077000000,"the year is yyyy and the day is dddd"));
//this year is 2013 and the day is Monday

alert(myDateTime.ToString('1/21/2014', "this month is MMMM and the day is dd"));
//this month is january and the day is 21

DEMO

您所要做的就是传递库js文件中包含的任何格式

日期比较:

var str1  = document.getElementById("Fromdate").value;
var str2  = document.getElementById("Todate").value;
var dt1   = parseInt(str1.substring(0,2),10); 
var mon1  = parseInt(str1.substring(3,5),10);
var yr1   = parseInt(str1.substring(6,10),10); 
var dt2   = parseInt(str2.substring(0,2),10); 
var mon2  = parseInt(str2.substring(3,5),10); 
var yr2   = parseInt(str2.substring(6,10),10); 
var date1 = new Date(yr1, mon1, dt1); 
var date2 = new Date(yr2, mon2, dt2); 

if(date2 < date1)
{
   alert("To date cannot be greater than from date");
   return false; 
} 
else 
{ 
   alert("Submitting ...");
   document.form1.submit(); 
} 
        from_date ='10-07-2012';
        to_date = '05-05-2012';
        var fromdate = from_date.split('-');
        from_date = new Date();
        from_date.setFullYear(fromdate[2],fromdate[1]-1,fromdate[0]);
        var todate = to_date.split('-');
        to_date = new Date();
        to_date.setFullYear(todate[2],todate[1]-1,todate[0]);
        if (from_date > to_date ) 
        {
            alert("Invalid Date Range!\nStart Date cannot be after End Date!")

            return false;
        }

使用此代码使用javascript比较日期。

谢谢D.吉普