Compare two date formats in javascript/jquery

前端 未结 6 818
庸人自扰
庸人自扰 2020-12-01 11:04

I have the following :

var fit_start_time  = $(\"#fit_start_time\").val(); //2013-09-5
var fit_end_time    = $(\"#fit_end_time\").val(); //2013-09-10

if(Da         


        
相关标签:
6条回答
  • 2020-12-01 11:22

    It's quite simple:

    if(new Date(fit_start_time) <= new Date(fit_end_time))
    {//compare end <=, not >=
        //your code here
    }
    

    Comparing 2 Date instances will work just fine. It'll just call valueOf implicitly, coercing the Date instances to integers, which can be compared using all comparison operators. Well, to be 100% accurate: the Date instances will be coerced to the Number type, since JS doesn't know of integers or floats, they're all signed 64bit IEEE 754 double precision floating point numbers.

    0 讨论(0)
  • 2020-12-01 11:24

    Try it the other way:

    var start_date  = $("#fit_start_time").val(); //05-09-2013
    var end_date    = $("#fit_end_time").val(); //10-09-2013
    var format='dd-MM-y';
        var result= compareDates(start_date,format,end_date,format);
        if(result==1)/// end date is less than start date
        {
                alert('End date should be greater than Start date');
        }
    
    
    
    OR:
    if(new Date(start_date) >= new Date(end_date))
    {
        alert('End date should be greater than Start date');
    }
    
    0 讨论(0)
  • 2020-12-01 11:26

    try with new Date(obj).getTime()

    if( new Date(fit_start_time).getTime() > new Date(fit_end_time).getTime() )
    {
        alert(fit_start_time + " is greater."); // your code
    }
    else if( new Date(fit_start_time).getTime() < new Date(fit_end_time).getTime() )
    {
        alert(fit_end_time + " is greater."); // your code
    }
    else
    {
        alert("both are same!"); // your code
    }
    
    0 讨论(0)
  • 2020-12-01 11:28

    This is already in :

    Age from Date of Birth using JQuery

    or alternatively u can use Date.parse() as in

    var date1 = new Date("10/25/2011");
    var date2 = new Date("09/03/2010");
    var date3 = new Date(Date.parse(date1) - Date.parse(date2));
    
    0 讨论(0)
  • 2020-12-01 11:39

    To comapre dates of string format (mm-dd-yyyy).

        var job_start_date = "10-1-2014"; // Oct 1, 2014
        var job_end_date = "11-1-2014"; // Nov 1, 2014
        job_start_date = job_start_date.split('-');
        job_end_date = job_end_date.split('-');
    
        var new_start_date = new Date(job_start_date[2],job_start_date[0],job_start_date[1]);
        var new_end_date = new Date(job_end_date[2],job_end_date[0],job_end_date[1]);
    
        if(new_end_date <= new_start_date) {
          // your code
        }
    
    0 讨论(0)
  • 2020-12-01 11:41

    As in your example, the fit_start_time is not later than the fit_end_time.

    Try it the other way round:

    var fit_start_time  = $("#fit_start_time").val(); //2013-09-5
    var fit_end_time    = $("#fit_end_time").val(); //2013-09-10
    
    if(Date.parse(fit_start_time) <= Date.parse(fit_end_time)){
        alert("Please select a different End Date.");
    }
    

    Update

    Your code implies that you want to see the alert with the current variables you have. If this is the case then the above code is correct. If you're intention (as per the implication of the alert message) is to make sure their fit_start_time variable is a date that is before the fit_end_time, then your original code is fine, but the data you're getting from the jQuery .val() methods is not parsing correctly. It would help if you gave us the actual HTML which the selector is sniffing at.

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