In this paper a first-principles study of the electronic structure and stability of B2 Ti1-xNiHfx (x = 0.2, 0.4, 0.6) and B19′ Ti1-xNiHfx(x = 0, 0.5) alloys is presented. The calculations are performed by the plane-wave pseudopotential method in the framework of the density functional theory with the generalized gradient approximation. This paper calculates the lattice parameters, density of states, charge density, and heats of formation. The results show that the electronic structure and stability of B2 Ti1-xNiHfx change gradually with Hf content. However, Hf content has little effect on the electronic structure and stability of B19′ Ti1-xNiHfx. The mechanism of the effect of Hf content on martensitic transformation temperature of TiNiHf alloys is studied from the electronic structure.