Abu Dharr reported,

I asked, "Messenger of Allah, which mosque was built first?"He replied, "The Sacred Mosque."I asked, "Then which?"He said, "Then the Aqsa Mosque."I asked, "How long was there between them?"He answered, "Forty years. And the whole earth is a place of prayer for you, so pray wherever the time of prayer overtakes you."

Ibn Majah 753

Sahih

حدثنا علي بن ميمون الرقي حدثنا محمد بن عبيد ح و حدثنا علي بن محمد حدثنا أبو معاوية عن الأعمش عن إبراهيم التيمي عن أبيه

عن أبي ذر الغفاري قال

قلت يا رسول الله أي مسجد وضع أول قال المسجد الحرام قال قلت ثم أي قال ثم المسجد الأقصى قلت كم بينهما قال أربعون عاما ثم الأرض لك مصلى فصل حيث ما أدركتك الصلاة

ابن ماجه 753صحيح
Contents
Reference
Commentary
Ayat
Collection
Sunan Ibn Majah
Book
Mosques and congregations
Chapter
Which masjid was established first
Numbering
Darussalam 753
Ibn Majah 753