javascript check end date is greater than or equal

2020-03-03 05:03发布

Is it possible to check whether an end date is greater than or equal to a start date in Javascript? My dates are strings in the format 'dd/mm/yyyy'.

标签: javascript
10条回答
家丑人穷心不美
2楼-- · 2020-03-03 05:36

Just convert the string to date and use getTime method of Date object to compare it.

Example code

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

查看更多
小情绪 Triste *
3楼-- · 2020-03-03 05:37

try this

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");
}
查看更多
神经病院院长
4楼-- · 2020-03-03 05:41

If the string format ('dd/mm/yyyy') doesn't change, this function should work:

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

Or extend the 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
查看更多
5楼-- · 2020-03-03 05:43

check out this function

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楼-- · 2020-03-03 05:46
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;
查看更多
做个烂人
7楼-- · 2020-03-03 05:47

Try this,

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;

}

}
查看更多
登录 后发表回答