Where the story is told
The parable is in Surah al-Kahf, Quran 18:32-44. Allah tells the Prophet ﷺ to present to the people an example of two men. The Quran calls it an example, and does not say who the two men were or when they lived.
It comes after the verses in which Allah tells the Prophet ﷺ to keep himself patient with those who call upon their Lord morning and evening, and not to let his eyes pass beyond them, wanting the adornments of worldly life (Quran 18:28). The parable shows how misleading those adornments can be.