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

piyushsain
Tera Guru
Tera Guru

Hi,

Use new Date(). It will give you current date and time

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

Ankur Bawiskar
Tera Patron
Tera Patron

@Ajay Singh2 

you want to auto-populate the variable when form loads?

OR

you want to set the variable when some other variable changes?

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

@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

Hi I want to create a alert (the alert shows the sys_updated_on field value) by using client script. I am failing can you guide how it can achieve.