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.

Set current date time using client script

DivyaNarayG
Tera Contributor

Hi,

We have a scenario when state changes to Assigned, the assigned date field should get update with current date time.

I am trying to on change client script with below script but it has update with incorrect format.

Please suggest.

 

if (newValue == '1')
       {
        g_form.setValue('assigned_date',Date.now());
       }
8 REPLIES 8

mihirlimje867
Tera Guru

Hello Divya,

Could you try this :

 

if (newValue == '1')
       {
        var today_date = new Date();
        g_form.setValue('assigned_date', today_date);
       }
 
 
 

Valmik Patil1
Kilo Sage

Hello @DivyaNarayG ,

Try using below

var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
var strtDate = today_date_str;

Thanks,

Valmik Patil

Ankur Bawiskar
Tera Patron
Tera Patron

@DivyaNarayG 

try this if the field is date/time

if (newValue == '1') {
    var today_date = new Date();
    var today_date_time_str = formatDate(today_date, g_user_date_time_format);
    g_form.setValue('assigned_date', today_date_time_str);
}

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

Hi Ankur,

 

Thanku. But this sets the value in local timezone. How can we update the value with system timezone.