Cosmic evolution of bars in simulations of galaxy formation