View previous topic :: View next topic
|
Author |
Message |
SENTHIL MURUGAAN Warnings : 1 New User
Joined: 12 Jan 2013 Posts: 32 Location: India
|
|
|
|
Hi,
Is there any FUNCTION to achieve in cobol to convert date from YYMMDD to YYYYMMDD format.
Thanks,
Senthil |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
Any particular problem with posting a COBOL question in the COBOL section
I'll move it for you, save you making any effort won't it |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
All the intrinsic FUNCTIONs are documented in the manual for your release of COBOL. Have a look.
However, something to do what you want sounds "unlikely", as you need some rules to be able to pull the correct century out of the air. Intrinsic FUNCTIONs never have a "psychic day". |
|
Back to top |
|
|
Abhishek Saurabh
New User
Joined: 25 Aug 2010 Posts: 23 Location: Pune, India
|
|
|
|
This should work fine....
COMPUTE WS-DATE1 = FUNCTION DATE-TO-YYYYMMDD(130822) |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1787 Location: Bloomington, IL
|
|
|
|
Do you know how the century window is defined, Abhi-kun? If so, why didn't you explain it? |
|
Back to top |
|
|
Abhishek Saurabh
New User
Joined: 25 Aug 2010 Posts: 23 Location: Pune, India
|
|
|
|
Hi Akatsukami..
I will be glad to explain the "century window" used in the above function.. It's just that the original question was pretty simple and straight-forward, so I also gave a simple and straight answer..
Well.. for the benefit of anyone who is not clear about the function I am copying an excerpt from IBM's COBOL Language Reference
-------------------------------------------------------------------------------------
The DATE-TO-YYYYMMDD function converts argument-1 from a date with a
2-digit year (YYnnnn) to a date with a 4-digit year (YYYYnnnn). Argument-2,
when added to the year at the time of execution, defines the ending year of a
100-year interval, or sliding century window, into which the year of argument-1
falls.
The function type is integer.
Format
FUNCTION DATE-TO-YYYYMMDD(argument-1[,argument-2] )
argument-1
Must be zero or a positive integer less than 1,000,000.
argument-2
Must be an integer. If argument-2 is omitted, the function is evaluated
assuming the value 50 was specified.
-------------------------------------------------------------------------------------
In my example, I had not taken argument-2, hence the default of 50 was applicable..
Let me try to make it more clear through two examples:
Example.1:
COMPUTE WS-DATE1 = FUNCTION DATE-TO-YYYYMMDD(130822,110)
Here, 13 is the year but to determine the century it will add 110 to the current year, which comes out to be 2123 (2013+110), which will now be considered as the last year of the current century in effect, so the only possible value for year can be 2113 (since we are considering the century from 2024 to 2123)..
Example.2:
COMPUTE WS-DATE1 = FUNCTION DATE-TO-YYYYMMDD(130822,50)
(Please note 50 is also the default value if arg-2 is not specified)
Here, 13 is the year but to determine the century it will add 50 to the current year, which comes out to be 2063 (2013+50), which will now be considered as the last year of the current century in effect, so the only possible value for year can be 2013 (since we are considering the century from 1964 to 2063)..
I hope I have made this clear enough.... |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Abhishek Saurabh,
From any professional data environment, two-digit years are nuts, are they not?
If the dates are new dates anyway, for 97 years just using the current century will work.
If the date is a birth-date, the default "window" will not be enough.
I haven't checked but suspect that the date FUNCTIONs spend tedious amounts of time calling LE routines. This may not be a problem.
I can't see the point in using a FUNCTION just to save 12 seconds of coding. |
|
Back to top |
|
|
Abhishek Saurabh
New User
Joined: 25 Aug 2010 Posts: 23 Location: Pune, India
|
|
|
|
Bill Woodger,
Yes, I would agree that keeping date values with two-digit years is not a very good option. And I guess this has been realized by many in recent years. However, if one already has dates in this format, there got to be some way to convert. Using the above function is one way. Any other (better) way would definitely be more welcome..
To take the specific example of birth-date that you quoted....yes, the default window may not be enough. So, one would require to decide upon a relevant value of 'argument-2'.. and just to add, this value can be negative also, for example....
COMPUTE WS-DATE1 = FUNCTION DATE-TO-YYYYMMDD(130822,-50)
In this case, the century in effect will be 1864 to 1963.. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Abhishek Saurabh, you provided a simplistic answer to the obvious question. HOWEVER, you did not do the research to find out what kind of date needed to be converted -- you just replied. As such, without knowing all of the details, your answer is incorrect. Furthermore, without knowing all of the details (such as whether SENTHIL MURUGAAN is dealing with birth dates of people that are 21 years or older, for example) -- your revised answer is STILL incorrect. You cannot set the year window without knowing the impact of using that window.
Granted, SENTHIL MURUGAAN posted a really bad question -- the type of date, and the possible range that the 2-digit year represents should have been included in the original post -- but that does not excuse you, as a responder, from making sure that your answer is accurate. You have failed in doing this. |
|
Back to top |
|
|
SENTHIL MURUGAAN Warnings : 1 New User
Joined: 12 Jan 2013 Posts: 32 Location: India
|
|
|
|
Hi Abhishek,
Thanks for your suggestion. I will try with provided "COMPUTE WS-DATE1 = FUNCTION DATE-TO-YYYYMMDD" Syntax to check on it.
Robert,
I am dealing with Treatment undergone dates and not birth dates. Also i am not sure of the date range.
Thanks,
Senthil |
|
Back to top |
|
|
Abhishek Saurabh
New User
Joined: 25 Aug 2010 Posts: 23 Location: Pune, India
|
|
|
|
Hi Senthil,
You are welcome. Please try and let us know here if you could fulfill your requirement.
Hi Robert,
First of all, if my way of answering was not good enough, I am sorry.. Hopefully, I'll improve.
Now, as far as keeping my answer simplistic and general is concerned, that was intentional. The original question by SENTHIL MURUGAAN was simple "Is there a function to achieve a requirement", so I gave a simple answer with a simple example.. Then, to make the logic of century window more clear, I tried to be more descriptive (with additional examples).. Yes, the answer was again general because my intention was to make clear the syntax and the underlying logic.
Now, what is the exact requirement, what is the exact scenario, what is the possible range of dates, whether the dates are birth-dates or some historic dates or some futuristic dates, I don't know, and I don't think I need to know. I would leave it to Senthil to use this basic information for his specific case and try to fulfill his requirement, and if he is not able (or able) to do so he can definitely post the updates, and we can continue the discussion accordingly.. |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1787 Location: Bloomington, IL
|
|
|
|
SENTHIL MURUGAAN wrote: |
I am dealing with Treatment undergone dates and not birth dates. Also i am not sure of the date range. |
So it seems likely that you would want a second argument of zero, yes? |
|
Back to top |
|
|
SENTHIL MURUGAAN Warnings : 1 New User
Joined: 12 Jan 2013 Posts: 32 Location: India
|
|
|
|
Hi Abhishek,
I tried with "COMPUTE WS-OUT-DT = FUNCTION DATE-TO-YYYYMMDD(ws-inpt-dt)" Syntax as per your suggestion and it worked fine as expected.
WS-INPT-DT PIC 9(6)
WS-OUT-DT PIC 9(8)
Thanks a lot.
But i do not have idea on whether i need to specify second argument as Zero as stated by Akatsukami. At present I am not using 2nd argument since it is working fine. But can u pls explain whether second arguement as '0' is required?
Thanks,
Senthil |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
Try with a date before the year 2000, e.g. 981230
This may not be a problem to you now, as you may not receive such data, but perhaps a little forward thinking as to what will happen in the future when a century or millenium changes. |
|
Back to top |
|
|
SENTHIL MURUGAAN Warnings : 1 New User
Joined: 12 Jan 2013 Posts: 32 Location: India
|
|
|
|
Hi expat,
I tried with the below dates and it were fine. I am not sure upto which futuristic date it will work, but from below data it shows that it will surely work upto 2060. anyway i will be dealing with only dates in 20th century and not 19th century for my reqmt.
Code: |
INPUT DT: 180512
OUTPUT DT: 20180512
INPUT DT: 650613
OUTPUT DT: 19650613
INPUT DT: 690725
OUTPUT DT: 19690725
INPUT DT: 510726
OUTPUT DT: 20510726
INPUT DT: 981230
OUTPUT DT: 19981230
INPUT DT: 601230
OUTPUT DT: 20601230
|
Thanks,
Senthil |
|
Back to top |
|
|
Abhishek Saurabh
New User
Joined: 25 Aug 2010 Posts: 23 Location: Pune, India
|
|
|
|
Hi Senthil,
Good to know that the suggestion worked for you..
However, you appear to be confused about the 2nd argument and whether you need it or not.. Well, whether you need it or not depends on what range of dates you are expected to handle, and you need to be clear about the impact of using the 2nd argument..
As you said, you are not using anything in 2nd argument, that means it will take default value of 50 (as menioned in my earlier post), which means the current date (whatever it is) lies in the middle of the current century in effect, so all the dates 50 years before and after the current date should work fine.. However, if you are planning to use 0 for the argument, it would mean the function would work fine only for current date and past date, any future date won't be handled correctly..
So, as I said please be clear about the impact of using the argument value and be clear about the range of values your program is expected to handle.. That would avoid any errors/unexpected outputs.. |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2454 Location: Hampshire, UK
|
|
|
|
Please note that we are in the 21st century now - not the 20th century. |
|
Back to top |
|
|
|