If you are going to write it into a UDF anyway rewrite it from scratch.
Make it really simple.
Get the digits in the right order.
Use IBM bif's to convert to date.
Use SQL error trapping to handle the exceptions.
Do not try to calculate leap dates, etc on your own. Sure you can, but