I am assuming based off your category, your inquiring about an auto air filter. It is hard to give a specific time or mileage figure because the life of the air filter depends on how much debris it ingests.
A filter that lasts 20,000 or even 30,000 miles on a vehicle that's driven mostly on expressways may last only a month or two in a rural setting where the vehicle is driven frequently on gravel roads. Changing it annually or every 15,000 miles for preventative maintenance may be a good recommendation for the city driver, but not the country driver.
Regardless of the mileage or time, a filter should be replaced before it reaches the point where it creates a significant restriction to airflow. But when exactly that point is reached is subject to opinion.
Removing the filter and holding it up to a light will show you how dirty it is. If it's really caked with dirt, it obviously needs to be replaced. Trying to shake or blow the dirt out is a waste of time because too much of it will be embedded in the filter fibers.
NOTE: Many filters that appear to be dirty are in fact still good and do not really need to be replaced. So it's up to you. If you think it's dirty, replace it. If you don't think it's dirty enough to need replacing, then don't.