With the increase in intermittent renewable energy sources and integration of different energy sectors, energy systems are expected to become more distributed and complex. There are many optimization models for analyzing how the structure and operational conditions such distributed energy systems affect the economic, environmental, and societal frameworks they belong to. These models often give optimal solutions without regard to how the solutions could be reached taking into account a realistic time perspective. This paper describes a mixed-integer linear programming modelling approach that optimizes investments and operation over longer time horizons, evaluating cumulated costs and emissions. As examples, two regional energy systems in different climates are optimized, demonstrating the feasibility of the approach. The model can act as a planning tool or for assessment of different future scenarios where fossil sources or energy are gradually replaced by renewable ones.