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

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.

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);
}