Date and time validations

chiragpathak
Tera Contributor

Hi Community,

I have a requirement where I have a date and time field (DD:MM:YYY HH:MM:SS) so I need to add validation to it in such a way that even the time should not be in the previous minute cause I am able to achieve validation on the date such that its not a previous date but if I select todays date but two hours ago it lets me select it where as it should not work like that it should clear the value on that field. As well as the showFieldMsg is not working its not showing the info message like "The selected date cannot be in the past".

1 ACCEPTED SOLUTION
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@chiragpathak 

approach shared by @J Siva should work fine.

In case you want onChange client script then use this

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

    g_form.hideFieldMsg('u_datetime_field');
    var selectedDateTime = new Date(getDateFromFormat(newValue, g_user_date_time_format)).getTime();
    var nowTime = new Date().getTime();
    if (nowTime > selectedDateTime) {
        g_form.showFieldMsg('u_datetime_field', 'The selected date cannot be in the past', 'error');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader