Reputation: 57
In my project I have a calculation like this Loan Start Date : Should be Start Date + 5 Working days. I was able to get current date, but dont know how to do that working days part. Can anyone help me.
Upvotes: 1
Views: 11334
Reputation: 154
I'm going to assume you're not working in a new shop. That being said, there should be a library of functions available that people have written for these kinds of tasks - surely yours isn't the first application that needed to calculate a date using working days. I've been in a few shops and every one of them has had a date function available so I've never had to bother with the details of date manipulation.
If you do end up needing to create your own logic you would do everyone a favor by creating a function, and then you're not bound to implementing this in COBOL.
Upvotes: 4
Reputation: 782
You can also perform the same using below SYSDUMMY1 query:
SELECT (CURDATE()+5 days)
FROM SYSIBM.SYSDUMMY1
WITH UR;
refer DB2 Basics: Fun with Dates and Times
Upvotes: 0
Reputation: 16928
Although easy enough to state, your problem is actually fairly complex to implement.
Generally COBOL compilers support a number of intrinsic functions that you can employ to perform simple date arithmetic. INTEGER-OF-DATE and DATE-OF-INTEGER are two examples. INTEGER-OF-DATE takes a date in 'YYYYMMDD' format and converts it to a number of days from a reference date, DATE-OF-INTEGER takes a number of days from the same reference date and converts it into a 'YYYYMMDD' format. It is a simple matter to buiild a third routine to perform simple date artithmetic using these functions. So far so good.
The hard part of your problem is determining: 5 business days. Typically, week ends and certain holidays are not counted as "business days" so the actual number of days you need to add can vary from 5 to quite a few more than 5. For example, if weekends are not to be counted and the day given to your calculation is a Friday, then you need to add 5 + 2 = 7 days to account for Saturday and Sunday not being business days.
If week ends are all you need to worry about, then it is not all that difficult to work out what day of the week a given day falls on using Zeller's Congruence. Then, based on the starting day of the week, you can easily determine if you need to add 5, 6 or 7 days to the starting date.
The really hard part is determining the other "non business days". Generally you need a lookup table of some sort for this simply because localization determines what these will be; and the actual day of the year varies for several holidays (eg. second Monday of the month). There is no simple way to get this part of the job done. In the systems I have worked on we use an in-house standard routine that applications call to obtain a "non-business" date determination. The routine takes a date and a localization parameter as input and returns a boolean Yes/No depending on whether the given date is a non-business date for the given localization. I suspect you will need to implement something similar. It is important that you use some sort of corporate wide standard routine for determining non-business dates to ensure all of your systems are in accordance with each other (not so good if system "A" determines that a given date is a non-business day when system "B" treats it as a regular business day)
Upvotes: 2
Reputation: 464
Check out this code example for how to write your own date handling routine.
http://www.cobug.com/cobug/docs/futureDateCalc.html
It assumes you're able to figure out what day of the week you're on now. (Use the INTEGER OF DATE function, divide what you get by 7 and the remainder is the day of the week - 1 for monday through 7 for sunday.)
If the Start Date always equals the same day you actually run the code, you could also do ACCEPT (a PIC 9 variable) FROM DAY-OF-WEEK and you'll get 1 for monday through 7 for sunday. This only works for the current date though.
Upvotes: 0
Reputation: 4263
Check your compiler documentation. You should have a data format that is number of days since the first of the year, or number of days since some arbitrary date (Jan 1, 1900 and Jan 1, 1600 are popular choices).
There should be a way, using your compilers runtime support libraries to convert the result of "Function Current-Date" into one of these forms. Then, addition and subtraction of days is easy (or easier). Once you have added the days, convert the result to whatever format you need.
Alternatively, you can write your own date handling routines. You just need to implement a 'carry' for months and years in the even you cross a month or year boundary.
Upvotes: 0