- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 09:09 AM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 12:02 PM
//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
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 09:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 10:44 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 12:02 PM
//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
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2019 12:25 PM
I knew it had to be something easy like this. Much thanks!