In this paper, we study the finite-time convergence of the time-varying dynamical systems for solving convex and nonconvex optimization problems in different scenarios. We first show the asymptotic convergence of the trajectories of dynamical systems while only requiring convexity of the objective function. Under the Kurdyka–Łojasiewicz (KL) exponent of the objective function, we establish the finite-time convergence of the trajectories to the optima from any initial point. Making use of the Moreau envelope, we adapt our finite-time convergent algorithm to solve weakly convex nonsmooth optimization problems. In addition, we unify and extend the contemporary results on the KL exponent of the Moreau envelope of weakly convex functions. A dynamical system is also introduced to find a fixed point of a nonexpansive operator in finite time and fixed time under additional regularity properties. We then apply it to address the composite optimization problems with finite-time and fixed-time convergence.