Apply Date Formatting

DevYadav
Tera Contributor

Hi All,

I want to apply date Formatting such that if it 1 then it should be "1st", 2 for "2nd ", 3 for "3rd" and remaining all for "4th or 5th"

i am using this code,

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]+"th-"+name+"-"+newValue[2];

g_form.setValue('u_date2',date2);

Thanks 

 

2 ACCEPTED SOLUTIONS

Hello @DevYadav 

 

I got you, your date format is some what different, no worries :-

 

 

 

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 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);
}

 

 

 

This will Definitely Works, Plz try it.

 

 

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

 

Regards,

Samaksh

 

 

View solution in original post

Hello @DevYadav 

 

Use this script for your requirement :-

 

 

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 dateM = newValue[2].split("");
var date2;
if((dateM[0]==0 || dateM[0]==2||dateM[0]==3) && (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);
}

 

 

 

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

 

Regards,

Samaksh

View solution in original post

13 REPLIES 13

Hello @DevYadav 

 

It will not run everytime, It will only run when the dates have th like 25th 18th.

 

Is your solution is working fine ??

 

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

 

Regards,

Samaksh

Hi @Samaksh Wani, it is showing th to every number

DevYadav_0-1690294192815.png

 

 

Hello @DevYadav 

 

I got you, your date format is some what different, no worries :-

 

 

 

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 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);
}

 

 

 

This will Definitely Works, Plz try it.

 

 

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

 

Regards,

Samaksh

 

 

thanks @Samaksh Wani, it worked!!!

Hello @DevYadav 

 

I have added the working script on the question of your second account also.

 

Can you pls mark the script as response as Accept on that question as well, it will help future readers to find correct response in the hierachy of conversation.

 

Regards,
Samaksh