Get started with Rowy in minutes
Dealing with dates, time, and timezones is one of the most painful experiences a developer can have: you need to take into account the user's timezone, daylight saving times, different encodings, datetime validation, misconfigured local times… and don't get me started on aggregating datetime data!
A backend solution like Google Firestore is a game-changer in this regard because it provides built-in best practices out of the box. In this article, we will cover everything you need to know to avoid complications with your timestamps down the road.
A timestamp is an encoded representation of a datetime object as an integer: the number of milliseconds since January the 1st, 1970, 12:00 PM UTC .
When you want to have a datetime in Javascript, you write the following:
const new_date = new Date()
// returns a date object
The best way to represent this information to store it in a database is using a timestamp, an integer:
new_date.getTime()
// returns a timestamp, e.g 1669386691734
As we already mentioned, timestamps represent the most efficient way to store datetime data. They are just integers so they take less space than text and you don’t need to parse or serialize them. Time is complex, so we might as well not make it more complicated.
If you want to set the property value of a given document to a timestamp, you’ll need to use the serverTimestamp
helper function:
import { serverTimestamp } from 'firebase/firestore'
const updated_at_timestamp = serverTimestamp()
docRef.update({ updated_at: updated_at_timestamp })
A Firebase CMS like Rowy automatically appends and updates createdAt and updatedAt fields:
Whenever you encounter a use case when you need to compare two timestamps in your application, you can use the Timestamp.fromDate() function to convert a native Date object in Javascript to Firestore’s Timestamp object. Firestore’s timestamps use nanosecond precision, whereas Javascript datetimes use millisecond precision:
import { Timestamp } from 'firebase/firestore'
const current_timestamp = Timestamp.fromDate(new Date())
if(!my_data.updated_at.isEqual(current_timestamp)){ ... }
Since timestamps are integers, you can also simply use them in your where
and orderBy clauses to perform comparisons inside database queries:
const res = await collection(firestore, "blog").orderBy('updated_at').get()
import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
const user_timezone = "America/Los_Angeles"
updateProfile(auth.currentUser, {
timezone: user_timezone
})
Assuming your user’s timezone based on browser data is often a bad practice―many users rely on firewalls and proxies to hide their location. Instead, offer the ability for users to define their timezone data themselves.
When you need to display a timestamp in the local timezone using Javascript, you can simply write:
const timezoned_date = new Date().toLocaleString('en', {
timeZone: auth.currentUser.timezone
})
With Rowy, you can use derivative columns to run scripts based on sibling columns. Say you are building a gardening app and you are measuring how many days it takes for a plant to grow. You can send a first request to register a start time, and a second request to register when the plant has fully matured. You can program a derivative column maturation
to automatically calculate the time difference between the two points to get a duration:
const derivative:Derivative = async ({row, ref, db, storage, auth})=> {
const beg_timestamp = row.begAt.toDate().getTime()
const end_timestamp = row.endAt.toDate().getTime()
const milliseconds_per_day = 1000 * 60 * 60 * 24
const difference_in_days = (end_timestamp - beg_timestamp) / milliseconds_per_day
return difference_in_days
}
That’s the bare minimum you need to know to deal with timestamp in Firestore! Hope you found it useful. Before you leave, do give Rowy a try. It only takes a few minutes to get started, and it’s completely free and open-source.