How to get the date value in client script

vijay23
Tera Contributor

Hi 

 

i have a  oob date field opened_at

 

when I use below  code in onchange client script (onchange of preliminary action date field). 

var openedDate = new Date(getDateFromFormat(g_form.getValue('opened_at'), g_user_date_time_format));
alert (openedDate);
every time the output is  wrong- Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
 
Can anyone help me how to fix this?
my requirement is to allow in preliminary action date , to select the date in between opened date and today date.
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

  var openedDate = new Date(getDateFromFormat(g_form.getValue('opened_at'), g_user_date_time_format));
    var preliminaryActiondDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));
    if ((preliminaryActiondDate < openedDate) || (preliminaryActiondDate > todayDate)) {
        g_form.clearValue('u_preliminary_action_date');
        g_form.showFieldMsg('u_preliminary_action_date', getMessage('preliminary_action_date.info.message'), 'error');
    }

}
 
but its not working , 
 
 
 
 
1 ACCEPTED SOLUTION

Rohit99
Mega Sage

Hi @vijay23,

You may try with the following on change client script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var openedDate = new Date(g_form.getValue('opened_at'));
    var preliminaryActionDate = new Date(newValue);
    var todayDate = new Date();
    // your concern is date that why rounding hours to 0
    openedDate.setHours(0, 0, 0, 0);
    preliminaryActionDate.setHours(0, 0, 0, 0);
    todayDate.setHours(0, 0, 0, 0);
    if (preliminaryActionDate < openedDate || preliminaryActionDate > todayDate) {
        alert("Error");
        g_form.clearValue('u_glide_date_time_1'); ////istead of "u_preliminary_action_date"i used "u_glide_date_time_1"
    }
 
Rohit99_0-1722499584734.png

 

 

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

View solution in original post

4 REPLIES 4

SN_Learn
Kilo Patron
Kilo Patron

Hi @vijay23 ,

 

Please check the below:

https://servicenowguru.com/client-scripts-scripting/client-side-dates-in-servicenow/ 

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Sohail Khilji
Kilo Patron
Kilo Patron

try :

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var openedDateStr = g_form.getValue('opened_at'); 
    var openedDate = new Date(openedDateStr);
    var preliminaryActionDate = new Date(newValue);
    var todayDate = new Date();
    todayDate.setHours(0, 0, 0, 0);
    if (preliminaryActionDate < openedDate || preliminaryActionDate > todayDate) {
        g_form.clearValue('u_preliminary_action_date');
        g_form.showFieldMsg('u_preliminary_action_date', getMessage('preliminary_action_date.info.message'), 'error');
    }
}

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Rohit99
Mega Sage

Hi @vijay23,

You may try with the following on change client script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var openedDate = new Date(g_form.getValue('opened_at'));
    var preliminaryActionDate = new Date(newValue);
    var todayDate = new Date();
    // your concern is date that why rounding hours to 0
    openedDate.setHours(0, 0, 0, 0);
    preliminaryActionDate.setHours(0, 0, 0, 0);
    todayDate.setHours(0, 0, 0, 0);
    if (preliminaryActionDate < openedDate || preliminaryActionDate > todayDate) {
        alert("Error");
        g_form.clearValue('u_glide_date_time_1'); ////istead of "u_preliminary_action_date"i used "u_glide_date_time_1"
    }
 
Rohit99_0-1722499584734.png

 

 

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

Adn Akbar
Tera Contributor

use the below code for 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var g_user_date_format = g_user_date_time_format.split(' ')[0]; // Extract only the date part
var g_user_time_format = g_user_date_time_format.split(' ')[1]; // Extract only the time part if present

var openedDate = new Date(getDateFromFormat(g_form.getValue('opened_at'), g_user_date_time_format));
var todayDate = new Date();
var preliminaryActionDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));

if (isNaN(openedDate.getTime())) {
alert('Invalid opened_at date');
return;
}

if (isNaN(preliminaryActionDate.getTime())) {
alert('Invalid preliminary action date');
return;
}

if ((preliminaryActionDate < openedDate) || (preliminaryActionDate > todayDate)) {
g_form.clearValue('u_preliminary_action_date');
g_form.showFieldMsg('u_preliminary_action_date', getMessage('preliminary_action_date.info.message'), 'error');
}
}