Nowadays, wireless ad hoc networks become more and more popular and significant in many fields. However, the important applications of wireless ad hoc networks make them very attractive to attackers. Moreover, the deployment scenarios, the functionality requirements and the limited capabilities of these types of networks make them vulnerable to a large group of attacks, e.g., wormhole attacks concerned in this paper. We propose a time-based scheme for the purpose of preventing wormhole attacks in wireless ad hoc networks. The scheme includes two phases which are detection phase and location phase. By detection phase, the existence of wormhole attacks can be detected. By location phase, the wormhole nodes can be identified. Simulation results show that our scheme is feasible and efficient in preventing wormhole attacks.