The optimal control of time-varying nonlinear systems is an important research topic and faces many difficulties and challenges, but also has substantial benefits. In this paper, the output optimization of scalar time-varying nonlinear (STVN) system and 2-dimension time-varying nonlinear (2DTVN) system is considered. The zeroing dynamics (ZD) method, which can solve the time-varying problems flexibly and effectively, has been extensively used in recent years. By introducing the ZD method, the time-varying optimal system state is attained, and the controller with the optimal output is designed. Meanwhile, the direct dynamics (DD) method is presented to further illustrate the superiority of the ZD method. Theoretical analyses prove that the ZD method has a faster convergence rate (that is, exponential convergence). In addition, the effectiveness and feasibility of the ZD method for solving the output optimization of STVN and 2DTVN systems are verified by numerical experiments, and further substantiated via output optimization of a two-wheeled mobile robot.