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

Scenario 2: Prevent form submission if the entered date (in JST-normalized format) is in the past.

function onSubmit() {
var value = g_form.getValue('your_date_field');
if (!value) return true;

// Parse 'yyyyMMdd' into JS Date
var year = parseInt(value.substring(0, 4), 10);
var month = parseInt(value.substring(4, 6), 10) - 1;
var day = parseInt(value.substring(6, 8), 10);
var enteredDate = new Date(year, month, day);

var now = new Date();
now.setHours(0, 0, 0, 0);
var jstNow = new Date(now.getTime() + 9 * 60 * 60 * 1000);
jstNow.setHours(0, 0, 0, 0);

if (enteredDate < jstNow) {
g_form.addErrorMessage("The date cannot be in the past (JST).");
return false;
}
return true;
}

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

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