KB Article 10112
2013/01/25

Close
X
Contact Tech Support

 Email Tech Support
 (250) 655-1766
 [7:30 - 5:00 PST]
Your Meeting ID will be Provided by a Rep.
Tech Support
Tech Support Home
Software Updates
Knowledge Base Search
Recent KB Articles
Product FAQs
Code Samples
Documentation
Tech Blog
System Requirements
Installation Instructions
Support Testimonials
Contact us to...
Create a Ticket
Request a Free Demo
Suggest a New Feature
Submit Feedback
Upload a Large File
Other Services
Software Training
Professional Services

You're getting an unexpected result when WebSmart's datedif function calculates how many months elapsed between two dates. How does this datedif function calculate the number of months?

Product: WebSmart ILE Type: Frequently Asked Question

Calculating the elapsed time in months is complicated by the fact that there is no standard length of a month. A month can be 28, 29, 30, or 31 days long.

Because of this inconsistency, the datedif and datedifn function can give an unexpected result when a date falls on the 29th, 30th, or 31st of a month.

According to IBM's documentation, the date operations in RPG use the following rules:

  • When months or years are added or subtracted, the day portion remains unchanged if possible. For example, 2000-03-15 + %MONTHS(1) is 2000-04-15. If the addition or subtraction would produce a nonexistent date (for example, April 31), the last day of the month is used instead.
  • Any month or year operation that changes the day portion is not reversible. For example, 2000-03-31 + %MONTHS(1) is 2000-04-30 changes the day from 31 to 30. You cannot get back the original 2000-03-31 by subtracting one month.

              The operation 2000-03-31 + %MONTHS(1) - %MONTHS(1) becomes 2000-03-30.

  • The duration between two dates is one month if the later date minus one month gives the first date. For example, the duration in months (rounded down) between 2000-03-31 and 2000-04-30 is 0 because 2000-04-30 - %MONTHS(1) is 2000-03-30 (not 2000-03-31)

Rate This Article

Did this example help you to achieve your goal?
 Yes  No  Don't Know

Enter additional comments below.   If you want to hear back from us, include your contact information.

Email Address:
Comments:
Please enable JavaScript in order to rate this page.