Sunday, May 22, 2016

Add 'n' number of Months to any date

Scenario:



The scenario is to add months to the given date.

Let us see how to add n number of months to a date using e-script.

(i.e., Add 10 months to the current date)
Consideration:
Leap Year and non leap years also needs to be considered.
Siebel Behavior:
Before going into the script, we need to know how Siebel behaves first.

Month calculation in Siebel is from 0 - 11. i.e., 0 for Jan - 11 for Dec.

So, whenever we do month calculation. We need to add 1 to the output to get the correct month before converting to the required format. (month = expDate.getMonth() + 1)

In the case where there is no need to change format, no need to add 1 to the output.

var date_field = bc.GetFieldValue("Field Name"); //date to which month needs to be added
var date_field = bc.GetFieldValue("Month Field"); //months to add to to date field

var new_date = new Date(date_field);
var month = new_date.getMonth() + ToNumber(expMonths);
new_date.setMonth(month);    // now new_date has the

//format for DTYPE_DATE
month = new_date.getMonth() + 1;
var date = new_date.getDate();
var year = new_date.getFullYear();
new_date = month + "/" + date + "/" + year;

If you face any issues while implementing this solution do comment below.

Hope this post helps you!

More info will pop-out. Keep watching.

No comments:

Post a Comment