With fresh items, bacteria growth can be estimated by using an exponential equation. Very similar to calculating radiation half-life, but with an upward swing on the graph (see that damn algebra is good for something, so write a thank you to your school teacher).
Bacteria isn't the only limiting factor. Raspberries will melt into a fungus goo in a week while carrots seem to have this ability to go bad after an amount of time best calculated in dog years. So the item itself by its nature is going to affect the outcome.
Since the maths assumes a uniform starting number of bacteria, it isn't perfect and won't be 100 per cent unless the item is pasteurised, such as milk. These dates should be pretty accurate and have always been in my experience.
Canned foods are sterilised while being processed and seem to last for as long as someone is brave enough to eat them. I've heard stories of those emergency supplies lasting for decades. Its limiting factor is the slow chemical reactions of the food itself.
Since the only reason a sane person would eat 20-year-old canned beans is an apocalypse, and that's what it is for, you could theoretically live long enough to wish you bought fresher food.
And let's not forget preservatives.
Produce, also, can have genes to avoid pests and increase shelf life. So the expression 'apples to apples' isn't really true any more. I have had them go bad fast (farmers' market) or had them last forgotten in the back of the fridge (literally from a hotel desk) six-plus months.
It surely will come down to an educated guess and then a low-tech fridge test. Basically putting the item in numerous settings and then checking up on it to try to keep accuracy. The more packaged it is the better triangulated the date will be.
Generally, when we establish the shelf life of a product, it's based on the result of putting the product into the conditions it's going to experience sitting on a store shelf – temperature, light, etc.
We'll taste the product periodically up until the point it's starting to go bad (from a taste perspective – if a product is going to fail due to microbiological contamination, it's going to happen in days/weeks, not six months down the road).
Once it starts to go bad, we'll subtract off a safety margin, and that becomes the shelf life.
These are edited answers from Who determines the expiration dates on food items? which originally appeared on Quora: The best answer to any question. Ask a question, get a great answer. Learn from experts and get insider knowledge. You can follow Quora on Twitter, Facebook, and Google+.