- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:25 AM
I want to add a Current Date Time in On change Client Script in form Variable
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:35 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:38 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:32 AM
Hi,
Use new Date(). It will give you current date and time
Regards,
Piyush Sain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:32 AM
you want to auto-populate the variable when form loads?
OR
you want to set the variable when some other variable changes?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 05:38 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-24-2023 10:07 AM
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.