cancel
Showing results for 
Search instead for 
Did you mean: 

How to add month

0 Kudos

Hello,

How can we achieve functionality of adding months like if we two fields are of type date and we want to add them up.

Eg: var month1 = this.date.getmonth();

       var month2 = this.date2.getmonth();

so if month1 = 1 , month 2= 3;

total month = month1 + month2; i.e. 4 not 13... as this feild becomes date type i am getting 13 as a result.

If anyone can provide inputs on this.

Thanks and Regards,

Stuti

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member200567
Active Contributor
0 Kudos

Hi Stuti,

Try this.

totalMonth=Numeric.ParseFromString(month1)+Numeric.ParseFromString(month2);

Regards,

Fred

0 Kudos

Thanks Fred..

One more ques. How can we then assign total month to date feild??

Regards,

Stuti

former_member200567
Active Contributor
0 Kudos

Hi, Stuti,

        Hmm. Back to date?

       

        this.todate=Date.Create(this.date1.GetDay(),this.totalMonth,this.GetYear());

        But there , you need to check if this.totalMonth is greater than or equal 12, if it is , you have to add 1 to the year and you have to change your month back.

    

       Is that what you want?

       If not, can you please let me know your use case more in detail?

       Or you can find it by yourself about Data Types and Reuse Libraries in Studio Documentation.

       The following is for Date.

       7.2.5.1.5 Date (Reuse Library)

Regards,

Fred.

0 Kudos

Hello Fred,

The requirement is to add month to given date. Is ther any simple way by which it can be achieved through Library, or we need to write logic ??

Thanks and Regards,

Stuti

Paul_Ka
Participant
0 Kudos

Hi Stuti,

You can use AddDuration method. Something like this:

dateNow = Date.ParseFromString("20100215");
result = dateNow.AddDuration(ABSL:Duration.ParseFromString("P1M"));


this will result in date 2010-03-16.


The duration is represented as ISO 8601 format. For example P3D would be 3 days.


Regards,

Paul

0 Kudos

Hello Paul,

Thanks for your reply, but if we are getting the value of months dynamically then? Then how will use it?

Will that be "PmonthM" ?

Thanks and Regards,

Stuti

Paul_Ka
Participant
0 Kudos

You can build the duration string if the value of months is not constant.

dateOld = Date.ParseFromString("20150524");


month = dateOld.GetMonth(); //returns 05


var str = "P"+ month + "M";


dateNew = dateOld.AddDuration(ABSL:Duration.ParseFromString(str));

Cheers,

Paul

0 Kudos

Thanks Paul.