How to check if one DateTime is later than another in javascript

前端 未结 7 813
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 04:57

Through the form i am getting two values like

   Start datetime = \'01/12/2013 12:00:00 AM\' and
   End datetime = \'02/12/2013 12:00:00 AM\'.
相关标签:
7条回答
  • 2020-12-06 05:24

    You can use Date.parse as following

    if (Date.parse(datetimeStart) < Date.parse(datetimeEnd)) {} else {}
    
    0 讨论(0)
  • 2020-12-06 05:29
    //StartDate & EndDate two dates
    
    if (StartDate < EndDate)
       // code
    
    if you just want the dates, and not the time
    
    if (StartDate.Date < EndDate.Date)
        // code
    
    0 讨论(0)
  • 2020-12-06 05:32

    Asuming you received a date in Javascript Date format you need Date.parse() function or compare by comparison operators. It will return the milliseconds that have passed since 01/01/1970 00:00

    Somehow like this:

    if(Date.parse(datetimeStart) < Date.parse(datetimeEnd)){
       //start is less than End
    }else{
       //end is less than start
    }
    

    Here is a Fiddle

    0 讨论(0)
  • 2020-12-06 05:35

    Try this following code:

    function dateCheck() {
        var fDate = new Date("26/05/2013");
        var lDate = new Date("24/05/2013");
        if(fDate <= lDate) {
            alert("true");
            return true;
        }
        alert("false");
        return false;
    }
    
    0 讨论(0)
  • 2020-12-06 05:38
    var record_day1=fromDate.split("/");
        var sum1=record_day1[1]+'/'+record_day1[0]+'/'+record_day1[2];  
        var record_day2=toDate.split("/");
        var sum2=record_day2[1]+'/'+record_day2[0]+'/'+record_day2[2];  
        var record1 = new Date(sum1);
        var record2 = new Date(sum2); 
        if(record2 < record1)
        {
                alert("End date must be greater than start date");
                return false;
        }  
    

    Here we are splitting the date and then combining it for comparing it hope it will work thanks.....:)

    0 讨论(0)
  • 2020-12-06 05:39

    its really simple in javascript

    var startTime = new Date('01/12/2013 12:00:00 AM');
    var endTime = new Date('02/12/2013 12:00:00 AM');
    

    and then all you need to do is compare

    if( startTime < endTime){
       alert("start time is lesser");
    }
    

    More on this here

    0 讨论(0)
提交回复
热议问题