Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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
Tera 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

Hi @user_ms 

 

You need to convert :

 

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

*************************************************************************************************************
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/dratulgrover [ Connect for 1-1 Session]

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

Ankur Bawiskar
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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

GlideFather
Tera Patron

Hi @user_ms,

 

please refer to this:

Understanding GlideDate() and GlideDateTime() in ServiceNow

_____
Answers generated by GlideFather. Check for accuracy.

Musab Rasheed
Kilo Patron

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