Go by the expiration date printed on the package. For most OTC medicines, opening the container doesn't start a shorter countdown — storage is what actually shortens a medicine's life.
Yes — Sunscreen carries a printed expiration date and that's the date to go by. Sunscreen is a regulated OTC drug in the US. The FDA requires sunscreens to stay at their original strength for at least three years, so a bottle with no printed expiration date should be discarded three years after you bought it. One with a printed date goes by that date. Heat and direct sun degrade it faster than the date implies — if it has separated or changed texture, replace it rather than risk a burn. Not medical advice.
Below 77°F (25°C), out of direct sun — a beach bag in the sun is the fastest way to ruin it. Heat and humidity shorten a medicine's real life faster than the printed date suggests, which is why a bathroom cabinet is the worst place for it.
Throw it out if you notice: Separation into watery and oily layers; Grainy, clumpy, or runny texture; Colour or smell change; Past the printed expiration date. Not medical advice — ask a pharmacist if you're unsure.