Print out month using GlideDateTime from Date field

Sri56
Tera Contributor

Hi All,

 

We have a field 'document date' which is a date field and we have another field which is 'period' single line text field.

So value of document date takes from default value: javascript:new GlideDateTime().getDisplayValue()

ex: 2023-04-14

so using client script, we are trying to get only month value from it and its working only on onload not on onchange

code: 

function onChange(control, oldValue, newValue, isLoading) {
//if (isLoading || newValue == '') {
if (newValue != '') {
var dd = g_form.getDisplayValue('document_date');
alert(dd);
var result = dd.substring(5, 7);
alert(result);
g_form.setValue('period', result);
return;
}

//Type appropriate comment here, and begin script below

}

Sri56_0-1681451061222.png

attachment screenshot for your reference.

 

Please help us to work this code onchange as well.

 

Thanks!

Sri

2 ACCEPTED SOLUTIONS

Pavankumar_1
Mega Patron

Hi @Sri56 ,

try below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        var date = g_form.getValue('document_date');
        var arr = date.split("-");
        g_form.setValue('period', arr[1]);
    }
    var arr1 = newValue.split("-");
    g_form.setValue('period', arr1[1]);
}
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

Tony Chatfield1
Kilo Patron

Hi, I suspect your issue is that the 'new' content of the date\time field has no display value as it is effectively an unsaved string and so you can just reference newValue.
Also no need to disable the OOB check for is loading or new value is NULL

This syntax works for me in a PDI, although I didn't check population of the 'period' field, but as long as the field name is correct I see no issues.

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

     if(newValue) {
        alert(newValue);
        var result = newValue.substring(5, 7);
        alert(result);
        g_form.setValue('period', result);
        return;
    }   
}

 

View solution in original post

2 REPLIES 2

Pavankumar_1
Mega Patron

Hi @Sri56 ,

try below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        var date = g_form.getValue('document_date');
        var arr = date.split("-");
        g_form.setValue('period', arr[1]);
    }
    var arr1 = newValue.split("-");
    g_form.setValue('period', arr1[1]);
}
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Tony Chatfield1
Kilo Patron

Hi, I suspect your issue is that the 'new' content of the date\time field has no display value as it is effectively an unsaved string and so you can just reference newValue.
Also no need to disable the OOB check for is loading or new value is NULL

This syntax works for me in a PDI, although I didn't check population of the 'period' field, but as long as the field name is correct I see no issues.

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

     if(newValue) {
        alert(newValue);
        var result = newValue.substring(5, 7);
        alert(result);
        g_form.setValue('period', result);
        return;
    }   
}