How do I convert a date format

John H1
Tera Guru

I have a Script Include/Client Script that adds 27 months to a date field, the problem is it changes the date format to 2023-09-12 instead of 09-12-2023.

 

How can I update the client script to show the correct format?

1 ACCEPTED SOLUTION

John H1
Tera Guru

Find the solution... Added this line of code

g_form.setValue('certificate_expiration_date',formattedDate);

 

View solution in original post

5 REPLIES 5

Harish Bainsla
Tera Sage
Tera Sage

can you share your code

Harish Bainsla
Tera Sage
Tera Sage

see below code and make changes as per your need

function addMonthsToField(fieldName, monthsToAdd) {
var field = g_form.getControl(fieldName);

if (field) {
var fieldValue = g_form.getValue(fieldName);

if (fieldValue) {

var glideDateTime = new GlideDateTime();
glideDateTime.setDisplayValue(fieldValue);

glideDateTime.addMonthsLocalTime(monthsToAdd);

var formattedDate = glideDateTime.getDisplayValue();


g_form.setValue(fieldName, formattedDate);
}
}
}

John H1
Tera Guru

Here is the Client Script I'm using.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue === '')
{
g_form.clearValue('certificate_expiration_date');
}
var ga = new GlideAjax('myDateUtil'); 
ga.addParam('sysparm_name','getEndDate'); 
ga.addParam('sys_parm_issue_dt',newValue); 
ga.addParam('sys_parm_how_many_months',27); 
ga.getXML(cb);  
 
// the callback function for returning the result from the server-side code
function cb(response) {  
var answer = response.responseXML.documentElement.getAttribute("answer"); 
if(answer)
g_form.setValue('certificate_expiration_date',answer);
}
 
//Type appropriate comment here, and begin script below
 
}

Sunny3008
Tera Guru

Hello @John H1 ,

 

Kindly Update your Script include the Code as follows and try if you get expected output:

 

getEndDate: function() {
        var issue_date = this.getParameter('sys_parm_issue_dt');
        var days = this.getParameter('sys_parm_how_many_months');
        var temp=issue_date.split('-');
        issue_date=temp[2]+'-'+temp[1]+'-'+temp[0]

        var gdt = new GlideDateTime(issue_date);
       gdt.addDays(days);
     var end_date=gdt.getLocalDate().getByFormat('dd-MM-yyyy');
     return end_date
    },

 

If my answer solves your issue please mark it as Helpful 👍 and Accepted✔️ based on impact.

Thanks & Regards,

 

Sunny R