Back ./firestore

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 05 Jan 2019.

  // getting data (timestamp) from cloud firestore
  let time =;
  let date = time.toDate();
  let shortDate = date.toDateString();
  let shortTime = date.toLocaleTimeString();

    // print date to console

More resources on Firebase

Got any question? You wanna have a chat? Hit my inbox on twitter asap 😉