Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date time Control

KouassiP
Tera Contributor

Hello All , 

I have a field in date/time format (YYYY-MM-DD HH:mm:ss)

Could you please help me to affect the value to a variable and add some seconds to this value and send the result to a new field (In a catalog client script ) .

 

IE .  field 1 : 2025-10-22 12:43:00 .

X = field 1 .

Y = (X + 60 seconds) .

Field 2 = Y .

Both fields 1 and fields 2 are in date/time .

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@KouassiP 

so you want to add 1 minute and set in other variable?

if yes then do this in onChange of 1st variable

something like this but please enhance

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Get the value of the source date/time variable
    var originalDateStr = g_form.getValue('source_datetime_var'); // replace with your variable name

    // Convert ServiceNow date/time string to JS Date
    var originalDate = new Date(originalDateStr);

    // Add 1 minute (60,000 ms)
    originalDate.setMinutes(originalDate.getMinutes() + 1);

    // Format as yyyy-MM-dd HH:mm:ss (ServiceNow expects this for date/time variables)
    function formatDateTime(dateObj) {
        var yyyy = dateObj.getFullYear();
        var mm = String(dateObj.getMonth() + 1).padStart(2, '0');
        var dd = String(dateObj.getDate()).padStart(2, '0');
        var hh = String(dateObj.getHours()).padStart(2, '0');
        var min = String(dateObj.getMinutes()).padStart(2, '0');
        var ss = String(dateObj.getSeconds()).padStart(2, '0');
        return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;
    }

    var newDateStr = formatDateTime(originalDate);
    // Set the target date/time variable
    g_form.setValue('target_datetime_var', newDateStr); // replace with your variable name
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

3 REPLIES 3

GlideFather
Tera Patron

Hi @KouassiP,

 

what is your exact question please? you mentioned a catalog client script, can oyu share it with us? It will be easier for a review or any meaningful help to provide you..

 

Also, for anything related to time and manipulating it, read this: GlideDateTime - Global

you can use addSeconds(60); 

 

Let me know what is your blocker. and we can brainstorm totgether.

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Ankur Bawiskar
Tera Patron
Tera Patron

@KouassiP 

so you want to add 1 minute and set in other variable?

if yes then do this in onChange of 1st variable

something like this but please enhance

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // Get the value of the source date/time variable
    var originalDateStr = g_form.getValue('source_datetime_var'); // replace with your variable name

    // Convert ServiceNow date/time string to JS Date
    var originalDate = new Date(originalDateStr);

    // Add 1 minute (60,000 ms)
    originalDate.setMinutes(originalDate.getMinutes() + 1);

    // Format as yyyy-MM-dd HH:mm:ss (ServiceNow expects this for date/time variables)
    function formatDateTime(dateObj) {
        var yyyy = dateObj.getFullYear();
        var mm = String(dateObj.getMonth() + 1).padStart(2, '0');
        var dd = String(dateObj.getDate()).padStart(2, '0');
        var hh = String(dateObj.getHours()).padStart(2, '0');
        var min = String(dateObj.getMinutes()).padStart(2, '0');
        var ss = String(dateObj.getSeconds()).padStart(2, '0');
        return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;
    }

    var newDateStr = formatDateTime(originalDate);
    // Set the target date/time variable
    g_form.setValue('target_datetime_var', newDateStr); // replace with your variable name
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@KouassiP 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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