This paper has developed an accurate method for calculating the first and second derivatives of dynamic responses with respect to the design variables of structures subjected to dynamic loads. An efficient algorithm to calculate the dynamic responses, their first and second derivatives with respect to the design variables is formulated based on the Newmark-β method. The algorithm is achieved by direct differentiation and only a single dynamics analysis is required. An example is demonstrated with the new method proposed in this paper and the analytical method. The comparative numerical results show the new method is highly accurate compared to the analytical method.