I am using firebase for my chat application. In chat object I am adding time stamp using Firebase.ServerValue.TIMESTAMP
method.
I need to show the messa
Iterating through this is the precise code that worked for me.
querySnapshot.docs.forEach((e) => {
var readableDate = e.data().date.toDate();
console.log(readableDate);
}
Here is a safe method to convert a value from firebase Timestamp type to JS Date in case the value is not Timestamp the method returns it as it is
Works for Angular 7/8/9
import firebase from 'firebase';
import Timestamp = firebase.firestore.Timestamp;
export function convertTimestampToDate(timestamp: Timestamp | any): Date | any {
return timestamp instanceof Timestamp
? new Timestamp(timestamp.seconds, timestamp.nanoseconds).toDate()
: timestamp;
}
This code is work for me
<script src="https://www.gstatic.com/firebasejs/4.5.1/firebase.js"></script>
<script>
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
var reff = firebase.database().ref('message');
reff.on('value',haveData, haveerr);
function haveData(datahave){
var existval= datahave.val();
var chabi=Object.keys(existval);
for(var d2=0;d2< chabi.length;d2++){
var r=chabi[d2];
var exitval=existval[r].Message;
var exitval1=existval[r].Name;
var exit=existval[r].Email;
var exitval2=existval[r].Subject;
var timestamp=existval[r].timestamp;
var sdate=new Date(timestamp);
var Year=sdate.getFullYear();
var month=sdate.getMonth()+1;
var day=sdate.getDate();
var hh=sdate.getHours();
var mm=sdate.getMinutes();
var ss=sdate.getSeconds();
}
}
function haveerr(e){
console.log(e);
}
</script>
First Of All Firebase.ServerValue.TIMESTAMP
is not working anymore for me.
So for adding timestamp you have to use Firebase.database.ServerValue.TIMESTAMP
And the timestamp is in long millisecond format.To convert millisecond to simple dateformat .
Ex- dd/MM/yy HH:mm:ss
You can use the following code in java:
To get the timestamp value in string from the firebase database
String x = dataSnapshot.getValue (String.class);
The data is in string now. You can convert the string
to long
long milliSeconds= Long.parseLong(x);
Then create SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Now convert your millisecond timestamp to ur sdf format
String dateAsString = sdf.format (milliSeconds);
After that you can parse it to ur Date
variable
date = sdf.parse (dateAsString);
Working with Firebase Firestone 18.0.1 (com.google.firebase.Timestamp)
val timestamp = (document.data["timestamp"] as Timestamp).toDate()
For Firestore that is the new generation of database from Google, following code will simply help you through this problem.
var admin = require("firebase-admin");
var serviceAccount = require("../admin-sdk.json"); // auto-generated file from Google firebase.
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
var db = admin.firestore();
console.log(admin.firestore.Timestamp.now().toDate());