How to add current DateTime in onchange client script

Ajay Singh2
Tera Contributor

I want to add a Current Date Time in On change Client Script in form Variable

2 ACCEPTED SOLUTIONS

Peter Bodelier
Giga Sage

Hi @Ajay Singh2 

 

There you go:

var currentDateObj = new Date();  

var currentDateUsr = formatDate(currentDateObj, g_user_date_time_format);

g_form.setValue('datetime', currentDateUsr);

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

@Ajay Singh2 

Here is the syntax

Date variable

var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
g_form.setValue('variableName', today_date_str);

Date/time variable

var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_time_format);
g_form.setValue('variableName', today_date_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

View solution in original post

6 REPLIES 6

I used this client below script, it shows the string value(which I typed) and not shows the date

var abc = g_form.getValue("sys_updated_on");
alert("last updated on "+ abc);

Peter Bodelier
Giga Sage

Hi @Ajay Singh2 

 

There you go:

var currentDateObj = new Date();  

var currentDateUsr = formatDate(currentDateObj, g_user_date_time_format);

g_form.setValue('datetime', currentDateUsr);

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.