Date should be 2 hours later from current time

Lucky1
Tera Guru

Hello all,

 

There is a Date and time field on a catalog item.

So, user selected time should be greater than 2 hours from the current time.

 

So, can you please tell me how can I achieve this?

 

 

Regards,

Lucky

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

you can either use UI policy or onChange

Sample UI Policy

AnkurBawiskar_0-1741845127153.png

AnkurBawiskar_1-1741845209673.png

 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

sample onChange client script

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

    var start = g_form.getValue('variableName');
    g_form.hideFieldMsg('variableName'); // give here your variable name
    var minutes = 120; // 120 mins is 2 hours
    var dateGiven = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime() + minutes * 60000; // add 120mins to date and then compare
    var nowTime = new Date().getTime();
    if (dateGiven < nowTime) {
        g_form.showFieldMsg('variableName', 'Please select date/time after 2 hours from now', 'error');
        g_form.clearValue('variableName');
    }

}

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

Hello Ankur,

 

I have used on Change CS, but even if I select 3 hours later current time, the variable is getting cleared.

Work out:

I have selected tomorrow's date first as the field is not allowing to select today's date even after changing the time. Then, I have changed the time after 3 hours from selected time and then changed the date to today's date. Then the field value is cleared and also field message is not showing.

 

Can you please help

 

 

Regards,

Lucky

 

Hello @Lucky1 

Taken some reference from: https://stackoverflow.com/questions/31383993/javascript-add-hours-to-time and https://stackoverflow.com/questions/37058704/getdate-slice-confusion to develop below onChange Client script:

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

    // Get the selected date and time from your date and time field
    var selectedValue = g_form.getValue('select_date_and_time');

    // Ensure valid date format as I am getting GMT information in the timestamp
    var selectedDateTime = getDateFromFormat(selectedValue, g_user_date_time_format);

    // Checking if the date is invalid (returns NaN)
    if (isNaN(selectedDateTime) || !selectedDateTime) {
        //console.log("Error: Invalid date format. Received:", selectedValue);
        return; // Prevent further execution
    }

    var selectedDate = new Date(selectedDateTime);
    // Add 2 hours
    selectedDate.setHours(selectedDate.getHours() + 2);

    // Format to exclude timezone information - function below
    var formattedDateTime = formatDateTime(selectedDate);
    alert(formattedDateTime); // Display formatted date-time

    // Set the formatted value in the field
    g_form.setValue('select_date_and_time', formattedDateTime);
}

// function to format the date correctly
function formatDateTime(date) {
    var yyyy = date.getFullYear();
    var mm = ('0' + (date.getMonth() + 1)).slice(-2);
    var dd = ('0' + date.getDate()).slice(-2);
    var hh = ('0' + date.getHours()).slice(-2);
    var min = ('0' + date.getMinutes()).slice(-2);
    return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min; // Adjust based on SN format
}

 
Results:

vishal_jaswal_1-1742142841512.png

 

vishal_jaswal_2-1742142849874.png

The script is working when you want to change date and time multiple times:

vishal_jaswal_3-1742142871746.png

vishal_jaswal_4-1742142916491.png


Hope it helps!



 


Hope that helps!