Kenil Barvaliya
Kenil Barvaliya

Reputation: 119

How to get system Timestamp for Firebase? in kotlin

I am using Firebase pagging and therefore i need current timestamp for queries database in firebase ? can anyone knows how to get current timestamp from system?

FirebaseFirestore.getInstance().collection("news")
        .orderBy("timestamp",Query.Direction.DESCENDING)
        .startAfter(timestamp) // for here i need current system timestamp..?? how can i??
        .limit(4)
        .get()

Upvotes: 1

Views: 555

Answers (1)

Ferin Patel
Ferin Patel

Reputation: 3998

firebase.firestore.FieldValue.serverTimestamp()

If you want the date value of firebase.firestore.FieldValue.serverTimestamp() you can use .toDate(). See FireStore Timesteamp.

If you want the current Date as a timestamp you can use the following

For Firebase Functions

import admin = require('firebase-admin');

const todayAsTimestamp = admin.firestore.Timestamp.now()

For local projects

import { Timestamp } from '@google-cloud/firestore';

const myTimestampAsDate = Timestamp.now()

Upvotes: 1

Related Questions