How to compare date field value with current date in client script of scoped application?

PShaha
Tera Contributor

I have written bellow script: Not getting desired output 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}


var today= new Date();
var strtDate = formatDate(today, g_user_date_format);

var end = g_form.getValue('start_date');

	
if(end > strtDate)
{

g_form.showFieldMsg('start_date',"Date is in future", 'info', false);
}
else
{
g_form.showFieldMsg('start_date',"Date is in past", 'warning', false);

}
}
4 REPLIES 4

Ankita19
Tera Guru
Tera Guru

SumanthDosapati
Mega Sage
Mega Sage

Hi,

Can you try below sample code

var start = g_form.getValue('start_date');  //give your field name
var today = new Date();
today.setDate(today.getDate());
if (start.toString() <= today.toString()){
g_form.addErrorMessage("Planned Start Date cannot be less than 24 hrs ahead in future.");

return false;
}

 

Regards,

Sumanth

Hi Sumanth,

Thanks for your reply but it didn't work for me.

Hey, You can Try this

 

 var today = new Date();
    var dateField = new Date(g_form.getValue('date_field'));
    if (today > dateField ) {
        var proceed = confirm("The selected Date is in the past. Are you sure you want to continue?");
        if (!proceed) {
            return false;
        }
    }
 
You can modify the script accordingly. But make sure the format of the New date and the Date field is same. Please mark my answer as Helpful if you found it helpful.