I wanted to get firebase current timestamp, not able to find a way to get that. I want to save child as shown in the image. And for that I've to get timestamp and get the date accordingly. Please help...
-
do you mean you want to set server time as a key with that format (dd-mm-yyyy)? – Wilik Jul 26 '16 at 16:24
-
@wilik. Yes, exactly – Brijesh Kumar Jul 26 '16 at 17:20
-
Just a heads up , Firebase behavior for timestamps in Firestore has changed in the most recent version of Firestore 5.4.1. – TheBen Aug 25 '18 at 21:15
4 Answers
You can set the server time by using ServerValue.TIMESTAMP
which is a Map<String, String>
type with {".sv" : "timestamp"}
pair. When it's sent to the firebase database, it will be converted to a Long Unix epoch time like this 1469554720
.
So the problem is, you can't set this as a key directly. The best approach is to put the timestamp inside your object and use DatabaseReference.push()
to get the guaranteed unique key.
For example
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
String key = ref.push().getKey(); // this will create a new unique key
Map<String, Object> value = new HashMap<>();
value.put("name", "shesh");
value.put("address", "lucknow");
value.put("timestamp", ServerValue.TIMESTAMP);
ref.child(key).setValue(value);
If you want to save it with that format (dd-mm-yyyy), there's a hack but this is not recommended. You need to save it first (ServerValue.TIMESTAMP) to another temporary node, and then retrieve the timestamp before convert it into that format using Date
class.

- 7,630
- 3
- 29
- 35
-
thank you very much..I tried this and its working fine. I can do this way too. But again is it possible to use this ServerValue.TIMESTAMP as a key? – Brijesh Kumar Jul 27 '16 at 01:13
-
You're welcome. I think it's possible but there will be two API call, set the TIMESTAMP value first and then get the last key by using `Query.limitToLast(1);`, this is not recommended because there is a probability that two users inserted two TIMESTAMPs at the same time so you can't get the correct key. BTW, please mark my answer as accepted if you think so. :) – Wilik Jul 27 '16 at 08:09
Actually, you can use cloud functions, and get the timestamp via an HTTP request. The function is very simple.
exports.getTimeStamp = functions.https.onRequest((req, res)=>{
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ timestamp: Date.now() }));
});

- 312
- 3
- 9
-
2Technically that will get the timestamp of the node js server, which theoretically could be different than the server Firebase or Firestore Database is on. – Jonathan Apr 01 '20 at 03:08
-
1@Jonathan thanks for rising up the question well, it's a unix time stamp which is the number of seconds between a particular date and the Unix Epoch (00:00:00 UTC on 1 January 1970). so, technically, it does not change no matter where you are located on the globe, that's why it's widely used in computer systems nowadays. – abdoo_salem Jun 11 '20 at 21:10
-
1Google servers will be set correctly, but another server could have the wrong time, no matter if the timestamp is correct or not. – Jonathan Jun 12 '20 at 12:26
On Android I did it this way
index.js / serverside
const functions = require('firebase-functions');
exports.stamp = functions.https.onCall(() => {
var d = new Date();
console.log('TimeStamp_now : '+d.getTime());
return { timeStamp: d.getTime() };
});
someclass.kt / clientside
lateinit var functions: FirebaseFunctions
var ret :Long = 0
FirebaseApp.initializeApp(this)
functions = FirebaseFunctions.getInstance()
val returnFC = functions.getHttpsCallable("stamp").call()
returnFC.continueWith { task ->
val resultFC = task.result?.data as Map<String, Any>
resultFC["timeStamp"] as Long
ret = "${resultFC["timeStamp"]}".toLong()
val cal = Calendar.getInstance()
cal.timeInMillis = "$ret".toLong()
var date = DateFormat.format("dd-MM-yyyy", cal).toString()
Log.d("current date:", date)
Log.d("timeStamp_from_function :", "$ret")
resultFC
}
returnFC.addOnSuccessListener {
Log.d("OnSuccessListener :", "success")
}
returnFC.addOnFailureListener {
Log.d("OnFailureListener:", "failure")
}
I got the return and arrived at: TimestampConvert look great

- 678
- 5
- 14
in my experience(in TS and React )....
import firebase from 'firebase/compat/app'; //v9
export const getNowTimeStamp = () =>{
return firebase.firestore.Timestamp.now();
}
and after importing the method
import { getNowTimeStamp } from '..path/to/firebase.utils';
console.log( getNowTimeStamp().toMillis() );
o/p:1631264447901

- 29
- 3