Compare two dates

Hello everyone ,
I want to know, how I can make a comparison between two dates entered :

if (start_date >= end_date) then for example : ( start_date : ’ 30/06/2016 ’ , end_date: ’ 31/08/2015 ’ )
{ alert (Incorrect date); }

Your example looks like JavaScript so I’m basing my answer on that.

There’s not very good support in JavaScript for dates (either within SuiteCRM or the language itself) so you’ll unfortunately need to parse the date yourself. You could do something like (untested):


function parseDate(str){
    //Beware - safer to check cal_date_format first and base parsing on that
    var arr = str.split('/');
    var date = Date(arr[2],arr[1],arr[0]);
    return date;
}

The dates returned from this method can then be compared in the usual way.

start_date_ob = parseDate(start_date);
end_date_ob = parseDate(end_date);
if (start_date_ob >= end_date_ob){
    alert ('Incorrect date');
}

Thanks,
Jim

Thank you MR JIM for your replay .
So I have to write this code there in a js file, and to use this file in the file editviewdefs.php

Concerning the alert message, I must have once the two entered dates are wrong

i created a validateDate.js
$(document).ready(function() {
if (document.getElementById(“date_debut_contrat_c”).value >= document.getElementById(“date_fin_contrat_c”).value)
alert(“Incorrect entry of dates”);
});

but I get the message that after modification, by cons at the first creation I do not obteint message

in php you may also use the following example as a reference: (there are many more ways of achieving the same)

if (Date(‘Y-m-d’, strtotime($my_date_1)) < Date(‘Y-m-d’, strtotime($my_date_2)) ){ //make sure that the date format provided corresponds to your date format
//echo your message here
}

The following is using javascript:

var D1 = “03-05-2014”;
var D2 = “28-04-2014 00:00”;

if ((new Date(D1).getTime()) >= (new Date(D2).getTime())) {
alert(‘correct’);
} else {
alert(‘wrong’);
}

You can link this logic into the SuiteCRM form validation code using the addToValidateCallback function.