user13871738
user13871738

Reputation:

How to get current System Timestamp for Firebase? in Android 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: 4

Views: 919

Answers (2)

Mr. Disability
Mr. Disability

Reputation: 838

Ferin's answer led me in the right direction. This what worked for me in my current project using Kotlin when getting the timestamp value to store in firebase cloud firestore.

val createdAt = FieldValue.serverTimestamp()

If anyone is confused I can explain further. Happy coding everyone!

Upvotes: -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: 3

Related Questions