How do i take a date field, add 5 years, then put answer in a new field?

Tony98
Mega Expert

I've got an Asset table with a Purchase Date field.

I need to add a new field called Lifecycle Expiration.

I then need to take the Purchase Date, add 5 years, then put that date in the Lifecycle field (read only field).

How do i do this?

find_real_file.png

1 ACCEPTED SOLUTION

Here's the code that I used. In my example, I subtracted a week, but the logic is the same and you can just tweak it to your own needs. Note that I'm adding 1 to the month because January returns as 0.



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



//Type appropriate comment here, and begin script below


var start_date = new Date(g_form.getValue('SOURCE VARIABLE'));


start_date.setDate(start_date.getDate() - 7);



var month = start_date.getMonth() + 1;


var day = start_date.getDate();


var year = start_date.getFullYear();


var work_start;



if(month < 10){


        month = "0" + month;


}


if(day < 10){


        day = "0" + day;


}



work_start = month + "-" + day + "-" + year;



g_form.setValue('DESTINATION VARIABLE', work_start);



}





Value('DESTINATION VARIABLE', work_start);


   


}


View solution in original post

8 REPLIES 8

Shahed Shah1
Tera Guru

Hi Anthony



You can have the new Lifecycle field as a calculated field. By taking the purchase date field (e.g. current.date_field_name), creating a new GlideDateTime object from that and then use the GlideDateTime API for the actual calculation, then just return the new value.



Hope this helps


Shahid


jhahn
Mega Expert

Hi Anthony,



I've done similar things in the past in a client script. First I follow the scripting suggestion here: javascript - Add year to todays date - Stack Overflow



The problem is that will return a date/time. I just parsed it into an array to get the information I wanted and manipulated it into the appropriate order. May not be the most effective way to do it, but it got the job done.



I can get you example code, if you'd like, a little later.


if you could post that script later please


Sure thing!