The optical spectra of two-dimensional (2D) periodic systems provide a challenge for time-dependent density-functional theory (TDDFT) because of the large excitonic effects in these materials. In this work we explore how accurately these spectra can be described within a pure Kohn-Sham time-dependent density-functional framework, i.e., a framework in which no theory beyond Kohn-Sham density-functional theory, such as $GW$, is required to correct the Kohn-Sham gap. To achieve this goal we adapted a recent approach we developed for the optical spectra of 3D systems [Cavo, Berger, Romaniello, Phys. Rev. B 101, 115109 (2020)] to those of 2D systems. Our approach relies on the link between the exchange-correlation kernel of TDDFT and the derivative discontinuity of ground-state density-functional theory, which guarantees a correct quasi-particle gap, and on a generalization of the polarization functional [Berger, Phys. Rev. Lett., 115, 137402 (2015)], which describes the excitonic effects. We applied our approach to two prototypical 2D monolayers, $h$-BN and MoS$_2$. We find that our protocol gives a qualitative good description of the optical spectrum of $h$-BN, whereas improvements are needed for MoS$_2$ to describe the intensity of the excitonic peaks.