Sunlight, heat and moisture are the main culprits for fish food degradation. (I must also add at this point that ingredient type and it's chemical make up may also be involved, but getting into this may protract this thread into details that could be reserved for a totally new thread.
In our case, sunlight is probably not a concern unless the food container is affected by heat generated in a place where it is in direct sunlight. So, we are now mainly concerned with heat and moisture. Moisture can enter food containers when left open for longer than necessary to obtain the amount of food you want to feed. Moisture on fingers or hands can also contribute. The construction of the container, along with how it opens may also contribute.
Your idea of dating your foods is very good. I think this should be recommended for all hobbyists who maintain aquariums. I never buy a container of food that has visible dust on it. Look at the whole selection stock. If any containers in the row have dust on them (such as the ones deeper in the shelf, pass them by.
That be said, for the majority of standard food fare that is stocked by most stores, assume that the food was probably produced within the past 2-6 months. That may seem like a long time gap, but you must consider where and when that food has been before you decide to purchase. There are a lot of brand foods out there and the majority of them (in flake, granule, pellet, or tablet) are very good and have been manufactured with care.
If one purchased a good brand named food which is packaged in good shape and not collecting dust on a shelf, I would assume that this container should last at least 3 to 4 months. This of course would be determined by the size of the container, how much I feed to what type of fish and being carful when storing and the feed mechanics.
So, the bottom line to your main question of how long does fish food last?..........I don't know! That is a very relative question
I've outline ideas and considerations for standard dry foods. Now 3 years would be far too long to be of any value to feed and would probably cause more harm to your fish and tank water.
We didn't even get into frozen, dehydrated, freeze dried, live cultured, or homemade concoctions.
You started dating the purchase of your fish foods and that is a good start.