Check if date is at least 2 days greater than current date

will_forster
Tera Guru

My script works for counting the days. but if I set the month to earlier it still returns true

 

will_forster_0-1718631739402.png

the output is wrong as the month is set to January

 

var systemDays = new GlideDate();
var twoDaysTime = systemDays.getByFormat('dd/MM/yyyy');
twoDaysTime.addDays(2);



if(requestedCollectionDate > twoDaysTime) {
	
var dateMessage = "The date you have selected " + requestedCollectionDate + " is greater than " + twoDaysTime + "   ";
	
}

 

 

 

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this function, just pass the date you want to compare

function isStartDateValid(startDate) {
	var currentDate = new GlideDate();
	var startDateGDT = new GlideDateTime(startDate);
	var currentDateGDT = new GlideDateTime(currentDate);
	var duration = GlideDate.subtract(currentDateGDT, startDateGDT);
	var days = duration.getDayPart();
	if (days >= 2) {
		return true;
	}
	return false;
}
-Anurag

Yashsvi
Kilo Sage

Hi @will_forster,

Using client script (onSubmit):

 

 

function onSubmit() {
    var currentDate = new Date();
    var dateField = g_form.getValue('your_date_field');
    if (dateField) {
        var fieldDate = new Date(dateField);
        var futureDate = new Date();
        futureDate.setDate(currentDate.getDate() + 2);
        if (fieldDate <= futureDate) {
            alert('The date must be at least 2 days greater than the current date.');
            return false; 
        }
    }
    return true; 
}

 

 

Thank you, please make helpful if you accept the solution.