I asked, "Messenger of Allah, which mosque was first established on earth?"He replied, "The Sacred Mosque."I asked, "Then which?"He said, "Masjid al-Aqsa."I asked, "How long was the period between them?"He answered, "Forty years. Then wherever the time of prayer overtakes you after that, pray there, for the merit lies in it."
قلت يا رسول الله أي مسجد وضع في الأرض أول؟ قال: المسجد الحرام، قال: قلت: ثم أي؟ قال: المسجد الأقصى، قلت: كم بينهما؟ قال: أربعون سنة، ثم أينما أدركتك الصلاة بعد فصله، فإن الفضل فيه.
Bukhari narrated it in the Book of Prophets (3366) and (3425), and Muslim in the Book of Masjids (1:520), both through routes from Amash, who said, Ibrahim Taymi told us, from his father, from Abu Dharr, and he related it.In another wording, "Then wherever the time for prayer overtakes you, pray, for the earth is a masjid for you."