javascript检查结束日期大于或等于开始日期

时间:2011-05-26 08:00:00

标签: javascript

是否可以在Javascript中检查结束日期是否大于或等于开始日期?我的日期是'dd / mm / yyyy'格式的字符串。

10 个答案:

答案 0 :(得分:9)

试试这个

var startDate = "05/01/2011";
var endDate = "09/01/2011";
var regExp = /(\d{1,2})\/(\d{1,2})\/(\d{2,4})/;
if(parseInt(endDate.replace(regExp, "$3$2$1")) > parseInt(startDate.replace(regExp, "$3$2$1"))){
alert("greater");
}

答案 1 :(得分:4)

如果字符串格式('dd / mm / yyyy')没有改变,这个函数应该有效:

function endAfterStart(start,end){
  return new Date(start.split('/').reverse().join('/')) <
          new Date(end.split('/').reverse().join('/'));
}
alert(endAfterStart('05/01/2011','09/01/2011')); //=> true

或者扩展Date.prototype

Date.prototype.isBefore = Date.prototype.isBefore || function(dat){
   return this < dat;
}
new Date('05/01/2011'.split('/').reverse().join('/'))
   .before( new Date('09/01/2011'.split('/').reverse().join('/')) );  //=>true

答案 2 :(得分:2)

最简单的方法。

function endAfterStart(start, end) {
    var startDate = new Date(start);
    var endDate   = new Date(end);

    return endDate.getTime() >= startDate.getTime();
} 

答案 3 :(得分:1)

花了一些时间来查找,但JQuery使用DatePicker date-range实现了这个确切的功能。 (源代码也可以在链接中找到。)

Moment.js还可以使用diff函数很好地处理日期比较。

答案 4 :(得分:1)

function isDate(value)
            {
                var fromDate = document.getElementById("fromDate").value
                var toDate= document.getElementById("toDate").value
                //var curr_Date= new SimpleDateFormat("dd/mm/yyyy");


            var dateRegEx = null;
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);

            if (dateRegEx.test(fromDate)){
            }
            else{
                alert("Invalid from date");
                return false;
            }
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);
            if(dateRegEx.test(toDate)) {
            }
            else{
                alert("Invalid to date");
                return false;
            }
            var stDate = new Date(fromDate);
            var enDate = new Date(toDate);
            var compDate = enDate - stDate;
            //var fdate=enDate-curr_Date;

            if(compDate >= 0)
                return true;
            else 
            {
                alert("To Date cannot be smaller than From Date");
                return false;
            }




            /**/
        }

这也适用于闰年......以dd / mm / yyyy格式(不是任何其他格式)。

答案 5 :(得分:0)

查看此功能

function CompareDates() 
{ 
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(); 
} 
} 

答案 6 :(得分:0)

试试这个,

function isDateCompare(){

var leadDate = document.getElementById('strDate').value;

var closeDate = document.getElementById('strDateClosed').value;

var date1 = new Date();

date1.setFullYear(leadDate.substr(6,4),(leadDate.substr(3,2)-1),leadDate.substr(0,2));

var date2 = new Date();

date2.setFullYear(closeDate.substr(6,4),(closeDate.substr(3,2)-1),closeDate.substr(0,2));

if (date1> date2)
{

alert("Expected Closed date cannot be less than Lead date.");

return false;

}

else
{

alert("true");

return false;

}

}

答案 7 :(得分:0)

首先使用此函数将字符串转换为js中的Date类型:

function common_getDateFromUI(str) {
    var arr = str.split("/");
    var returnDate = new Date(arr[2], arr[1] - 1, arr[0], 0, 0, 0, 0);
    return returnDate;
}

第二:获得javascript日期类型后,只需将其作为普通类型进行比较,如date1&gt; date2或date1 == date2。
或者使用此函数获取日期之间的差异日期:

function CalendarDays(startDate, endDate) {
    if (endDate < startDate)
        return 0;

    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0, 0, 0, 1); // Start just after midnight
    endDate.setHours(23, 59, 59, 999); // End just before midnight
    var diff = endDate - startDate; // Milliseconds between datetime objects
    var days = Math.round(diff / millisecondsPerDay);

    return days;
}

点击此链接是一个简单的演示,可以在日期之间获得差异。 Link demo here

答案 8 :(得分:0)

if (iForm.DiddfromDate.value == "") 
  { 
    alert("  Please enter a value");  
    iForm.DiddfromDate.focus(); 
    return false;    
  }
     if (iForm.DiddtoDate.value == "") 
  { 
    alert("  Please enter a  value");  
    iForm.DiddtoDate.focus(); 
    return false;    
  }      
        try {
            var d1 = iForm.DiddfromDate.value.substr(0, 2);
            var m1 = iForm.DiddfromDate.value.substr(3, 2);
            var y1 = iForm.DiddfromDate.value.substr(6, 4);
            var StrDate = m1 + "/" + d1 + "/" + y1;

            var d2 = iForm.DiddtoDate.value.substr(0, 2);
            var m2 = iForm.DiddtoDate.value.substr(3, 2);
            var y2 = iForm.DiddtoDate.value.substr(6, 4);
            var EndDate = m2 + "/" + d2 + "/" + y2;

            var startDate = new Date(StrDate);
            var endDate = new Date(EndDate);
            if (startDate > endDate) {
                alert('To date should be greater than From date.');
                iForm.DiddfromDate.value = '';
                iForm.DiddtoDate.value = '';
                iForm.DiddfromDate.focus();
                return false;
            }
        } catch (e) { alert(e.Description); }
 return true;

答案 9 :(得分:0)

只需将字符串转换为日期并使用Date对象的getTime方法进行比较。

示例代码

var startDate = '04/04/2015'; //date in dd/mm/yyyy format
var endDate = '05/04/2015';

function compareDates(sDate, eDate) {
    var dateTime1 = new Date(sDate).getTime(),
        dateTime2 = new Date(eDate).getTime();
  var diff = dateTime2 - dateTime1;
  if (diff > 0) {
    alert("endDate is greater than startDate");
    return true;
  } 
}
compareDates(startDate, endDate);

Working Fiddle