How to Add the Month

hi All,

i have a small issue ,i.e i want to add month like i have 2 fields

new date = predate + 1; i.e

new Date = 201006 + 1 = 201007;

And more thing my fields are str fields

can u any one help me to add the date .

Regards

Nani

You need to actually convert them into number of days and need to add the days to your date…

I recomment to convert it to UtcDateTime and call DateTimeUtil::addMonths().

Hi Nani,

You should use the nextMth function to achieve what you require.

You can:

  1. Convert the string field to a date field by the use of str2date function.

date str2Date(str _text, str _sequence)

  1. Use the nextMth function to get the required date of the next month:

The nextMth function retrieves the date in the following month that corresponds most closely to a specified date.

For example, nextMth(29\02\1996) returns 29\03\1996, but nextMth(31\01\1996) returns 29\02\1996 (leap year).

date nextMth(date date)

  1. After using the nextMth function convert the date again to string by the use of date2str to store it in

the string type field.

str date2Str(

date date,

int sequence,

int day,

int separator1,

int month,

int separator2,

int year)

It might be a bit late to reply for this post. I hope it might help someone in future.

DateMthFwd(date transDate, int qty)

example: DateMthFwd(inventBatch.expDate, 12); This increases the expiry date to 12 months

http://msdn.microsoft.com/en-us/library/aa659686(v=ax.50).aspx

Thanks,

Jay

Actually Jay, your timing was near perfect!

And if anyone else finds this, here’s why you don’t want to use ‘NextMth’:

If you’re using a loop where you want to get the date for the next 12 months …something like this:

for (i = 1; i <= 12; i++)

{

//_startDate = nextMth(_startDate);

}

You start at say 6/30/2014 and each time it goes through the loop it increments one month, so 7/30/2014, 8/30/2014, 9/30/2014, 10/30/2014, 11/30/2014, 12/30/2014, 1/30/2015…AND THEN THE PROBLEM…2/28/2015, 3/28/2015, 4/28/2015, 5/28/2015, 6/28/2015.

Your end date should be 6/30/2015, but instead is 2/28/2015 because it changed it in February because there are only 28 days.

Using Jay’s solution (dateMthFwd) fixes that problem:

Date initialStartDate;

initialStartDate = _StartDate;

for (i = 1; i <= billingPeriodQty; i++)

{

_startDate = dateMthFwd(initialStartDate, i);

}

This will actually adjust to 28 days in February 2015, but go back to 30 days in March 2015 (in this example).

Hope this helps someone else.

Not to confuse with my above example…

Actually in my solution example, the loop isn’t necessary unless you are trying to return each month (there was some additional code after that I didn’t paste). My method is a little more complex than the following, but if you just wanted to return the date for next year (12 months from now), your method would look something like this:

private TransDate getEndDate(TransDate _startDate)

{

TransDate endDate;

endDate = dateMthFwd( _startDate, 12)

return endDate;

}