Using Timestamp To Filter And Order Firestore Documents

This dev doc guide is best suited for anyone who has been using firebase cloud firestore for a while i.e intermediate to expert level of experience.

If you are a beginner or would want to try out the magic with cloud firestore for web, get started in this codelab here 😎

βœ… ​Adding a Timestamp field to your documents

The code snippet shows below how to send some "fetched" or "submitted" data to Cloud Firestore, this could be inputs from a form. We add the timestamp ( time the update was done).

 // Add a new document with a generated id
        username: username,
        country: country,
        track: track,

β€‹βœ… Using Timestamp to order documents

Now let's sort and order our documents in the, "devs" collection. Let us order by timestamp and in descending order.

    .orderBy("timestamp", "desc").onSnapshot(snapshot => {      
        /* your magic 
         goes here */                  

Yey! We're done! Learn more about sorting and filtering data in cloud firestore in this awesome firebase doc

Advanced Recipe

You may want to actually at the same time format the date stored; here is how to play around with it in javascript to display for e.g 04/04/2019.

  // getting data (timestamp) from cloud firestore
    let postedDate =;
    var date = postedDate.getDate();
    var month = postedDate.getMonth(); // jan is 0 not 1
    var year = postedDate.getFullYear();

    // function to add 0 to a day or a month < 10
    function pad(n) {
        return n<10 ? '0'+n : n;
    // display date
    var dateString = pad(month + 1) + "/" + pad(date) + "/" + year;
    // print to console

Got any question? You wanna have a chat? Hit my inbox on twitter asap πŸ˜‰