What swuehl said and linked to - dates should be date fields, typically read in using the date#() function. And I would keep the calculation in script rather than the presentation layer as long as these two dates are on the same table. And that's my general philosophy - keep the complication in script except for aggregations. Do all your aggregating in the presentation layer.
Not sure which part of the if statement is giving you incorrect answers (true or false or both), but you mentioned you wanted to see 0 when release - delivery = 0. I think in your today's script, if the difference is 0, it will look for difference between today and delivery. You might want to change it to this:
If(Date.4.Release-Date.2.Delivery < 0, Today() - Date.2.Delivery, Date.4.Release - Date.2.Delivery) as Layover
Notice that I removed that equal (=) sign from the if statement's condition. I also removed the use of Num function as difference of two dates should be a number (i.e. if they are true date fields with dual orientation).