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

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

Thank you for your answer.

It is useful.

user_ms
Tera Expert

 

Additional

I want to get today's date and if a past date is entered, I want to get an error in the submit check.

Is this for a catalog item ?
Then I would suggest you do the validation through a Catalog UI policy to restrict the date from being in the past.
Add an Action to the Catalog UI policy which clears the variable if the policy evaluates to true (date is before today).

Example:

prohibit-date-before-today-catalog-ui-policy.png

kaushal_snow
Mega Sage

Hi @user_ms ,

 

Please go through below: populate a date field with today’s date in JST (yyyyMMdd format) when the form loads.

 

function onLoad() {

var now = new Date();

var jstDate = new Date(now.getTime() + 9 * 60 * 60 * 1000);

var yyyy = jstDate.getFullYear();
var mm = String(jstDate.getMonth() + 1).padStart(2, '0');
var dd = String(jstDate.getDate()).padStart(2, '0');
var formattedDate = yyyy + mm + dd;

g_form.setValue('your_date_field', formattedDate);
}

 

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/