The Doppler effect for electromagnetic waves results in either a redshift or blueshift of light and is of great use in astronomy. It has been used to measure the speed of stars and galaxies approaching or receding from the earth. Currently, all Doppler effect formulas only work for constant velocities. Hence, the application of the Doppler effect includes the implicit assumption of a constant velocity of the motion during the period from the light emission to measurement. Since the light from remote stars detected from Earth may be from millions of years ago, it is difficult to assume that these stars kept moving at constant velocity for the long period, which may become a potential concern. A time-varying Doppler effect formula is mathematically derived from the principle of constant light speed, which is consistent with the classical and the redshift formulas. This formula is also supported by existing experiments and theoretically proved by Maxwell’s wave equations. The potential application of this time-varying Doppler effect formula in cosmology is discussed with the examples of cosmological redshift and Hubble’s law. The paper shows that the cosmological redshift can be interpreted as a special case of the time-varying Doppler effect. Further discussion between the observed Hubble’s redshift relationship and Hubble’s law may be needed.