timestampDiff

Pipeline expression helper that returns the difference between two timestamps in a chosen unit.

timestampDiff() calculates how much time separates an end timestamp from a start timestamp, expressed in the unit you choose (day, hour, minute, and so on).

Import from the pipelines entry point:

js
import { timestampDiff, field } from '@react-native-firebase/firestore/pipelines';

Basic example

js
import { getFirestore } from '@react-native-firebase/firestore';
import { execute, field, timestampDiff } from '@react-native-firebase/firestore/pipelines';

const db = getFirestore('your-enterprise-database-id');

const snapshot = await execute(
  db
    .pipeline()
    .collection('events')
    .select(
      timestampDiff(field('endTime'), field('startTime'), 'day').as('daysApart'),
      timestampDiff('endTime', 'startTime', 'hour').as('hoursApart'),
    ),
);

snapshot.results.forEach(row => {
  console.log(row.data().daysApart, row.data().hoursApart);
});

Related helpers

HelperUse when
timestampDiffMeasure elapsed time between two timestamps
timestampAddShift a timestamp forward by an amount
timestampSubtractShift a timestamp backward by an amount
TimeUnitType for 'microsecond''day' unit literals

Upstream reference

See the firebase-js-sdk timestampDiff declaration for full overload signatures.

On this page