Current DateTime in Client Script

juriggs
Kilo Expert

I'm trying to return the current date so I can make sure a change request isn't schedule for the past in a client script. I've tried to use GlideDateTime, but my research has shown that this is a server side object.

 

In one of the threads on this topic it says "what you should do here is: call a script include from here, in the script include user GlideDateTime and return the value to client. Use GlideAjax to call Script include."

That sounds great and all, but I'm brand new to developing on the ServiceNow platform. Could somebody break down how this would work in a bit more detail to point me in the right direction?

Thanks.

1 ACCEPTED SOLUTION

//java script date object

 

var d = new Date();

useful links 

 

https://www.w3schools.com/jsref/jsref_obj_date.asp

https://www.w3schools.com/js/js_date_formats.asp

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

4 REPLIES 4

Nitin_NOW
Tera Guru

You can create an onSubmit() Client script on change_request table to check the date validation. Below is the script you can use 

function onSubmit() {
var startDate = g_form.getValue("start_date");
var endDate = g_form.getValue("end_date");
var format = g_user_date_time_format;

if (startDate === "" || endDate === "")
return true;

// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);

if (startDateMs < endDateMs)
return true;

g_form.clearMessages();

// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDateMs === 0 || endDateMs === 0) {
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start_date")));

if (endDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("end_date")));

return false;
}

if (startDateMs > endDateMs) {
g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));

return false;

}
}

Please hit correct based on impact of response.

Thanks 

Nitin

Hi Nitin,

 

Thanks for this, but what I'm trying to do is compare the start_date field value with the current date (not the end_date). Grabbing the start date isn't a problem, but there doesn't seem to be a way to generate the current date on the client side.

 

I come from a Powershell background, and I would just say Get-Date, which would return me an object with today's date. Is there nothing like that in client side Javascript?

//java script date object

 

var d = new Date();

useful links 

 

https://www.w3schools.com/jsref/jsref_obj_date.asp

https://www.w3schools.com/js/js_date_formats.asp

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

juriggs
Kilo Expert

I knew it had to be something easy like this. Much thanks!