separate dates using split function

DEV976
Tera Expert

Hi All, 
i am new to split function. 
I want to separate date, month and year from dd-mm-yyyy format on catalog item and show just month at there.

there are 2 variables. one is date [dt] and second is show date[u_sd]. the requirement is: autopopulate date on show date variable and the date that is present on show date, it's month must be convert into name, like jan for 1 or march for 3 etc.

thanks

2 ACCEPTED SOLUTIONS

Ranjit Nimbalka
Mega Sage

hello @DEV976 ,

write a onchange client script on first date field and try code like below.

 

you can do something like below:-

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var months=["jan","feb","march","april","may","june","july","sep","october","november","december"];
newValue= newValue.split("-",[3]);
var month=newValue[1];
var name=(months[month-1]);
var date2= newValue[0]+"-"+name+"-"+newValue[2];
g_form.setValue('u_date2',date2);
//Type appropriate comment here, and begin script below
 
}
 
 
Screenshot 2023-07-21 at 11.48.54 PM.png
 
Regards,
Ranjit

 

View solution in original post

Hello @DEV976 

 

 

 

 

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

var months=["jan","feb","march","april","may","june","july","sep","october","november","december"];
newValue= newValue.split("-",[3]);
var month=newValue[1];
var name=(months[month-1]);
var dateM = newValue[2].split("");
var date2;
if((dateM[0]==0 || dateM[0]==2) && (dateM[1]==1)) {
   date2= newValue[0]+"-"+name+"-"+newValue[2]+"st";
}
else if((dateM[0]==0 || dateM[0]==2)&& (dateM[1]==2) ){
   date2= newValue[0]+"-"+name+"-"+newValue[2]+"nd";
}
else if((dateM[0]==0 || dateM[0]==2) && (dateM[1]==3) ){
   date2= newValue[0]+"-"+name+"-"+newValue[2]+"rd";
}
else{
 date2= newValue[0]+"-"+name+"-"+newValue[2]+"th";
}
g_form.setValue('u_date2',date2);
}

 

 

 

 

I have tried on my pdi, it is working fine.

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

View solution in original post

18 REPLIES 18

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @DEV976 

You can use getMonth() method 

Try var date = new Date();

var Month = date.getMonth();

please mark the answer as helpful and correct based on Impact!!

Kind Regards,

Ravi Chandra.

 

 

Hi @Ravi Chandra_K, Good Day!
i've update the requirement. actually i want a complete code for this

thanks

Hello @DEV976 

You can write on change client script on first date.

to get the month name, refer the article below:

https://www.servicenow.com/community/developer-articles/scripts-glidedate-getdate-get-month-name-exe...

please mark the answer as helpful and correct based on Impact!!

 

Kind Regards,

Ravi Chandra.

Hello @DEV976 

 

You can use the OnChange Catalog Client Script for autopopulating the value on the basis of first field.

 

 

var date = g_form.getValue('dt') // first variable name;
var newDate = date.getMonth();
g_form.setValue('u_sd', newDate);

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh