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.

How to way Today date get at Client Script (JST/yyyymmdd)

user_ms
Tera Expert

I'd like to get today's date using a client script.
If anyone knows how, please let me know.

 

The conditions are as follows:
・Get in Japan time(JST)
・Get in yyyymmdd format

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

You can easily get and display the users time through the javascript Date() object.

A simple example:

   var date = new Date();
    g_form.addInfoMessage('Current date: ' + date.getFullYear() + (date.getMonth()+1) + date.getDate());

// the +1 to the month is because the months are numbered as 0-11

View solution in original post

10 REPLIES 10

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @user_ms 

 

You need to convert :

 

https://www.servicenow.com/community/developer-blog/converting-date-time-into-any-time-zone-in-servi...

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@user_ms 

what's your actual business requirement?

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

GlideFather
Tera Patron

Hi @user_ms,

 

please refer to this:

Understanding GlideDate() and GlideDateTime() in ServiceNow

_____
This reply is 100 % GlideFather and 0 % AI. Disaster may appear. Deal with it!

Musab Rasheed
Tera Sage

Try below script

function getTodayInJST() {
  // Get current local time
  var now = new Date();

  // Convert to UTC by adjusting with local timezone offset
  var utc = now.getTime() + (now.getTimezoneOffset() * 60000);

  // JST is UTC + 9 hours = 9 * 60 * 60 * 1000 ms
  var jstOffset = 9 * 60 * 60 * 1000;

  // Create a new Date object for JST
  var jstDate = new Date(utc + jstOffset);

  // Format components to yyyyMMdd
  var year = jstDate.getFullYear();
  var month = ('0' + (jstDate.getMonth() + 1)).slice(-2);
  var day = ('0' + jstDate.getDate()).slice(-2);

  return '' + year + month + day;
}

// Example usage in a client script
function onLoad() {
  var jstToday = getTodayInJST();
  console.log('Today (JST) in yyyymmdd:', jstToday);
  // You can also set it on a field, e.g.:
  // g_form.setValue('your_date_field', jstToday);
}
Please hit like and mark my response as correct if that helps
Regards,
Musab