Get timestamp from Firebase and convert it into Javascript date object - javascript

firebase realtime database
I manage to fetch the timestamp data from firebase and save it into a variable, currentTime.
The timestamp is saved in string type in firebase as shown in the picture.
I want to convert it into Javascript object type.
So, let jsTime = new Timestamp(currentTime);
And I tried to console.log(jsTime.toDate()) but the console shows 'invalid Date'.
Please help me to solve this problem.

Try it with the following code
let date = new Date(jsTime);

Related

How to pass a JS date object to cloud function for storing it on Firestore

When I write a Date object to my Firestore, all works correctly and I can see a date in the DB stored with the type "timestamp". If I try to pass a Date object to a cloud function, I don't know why but it is an empty object when I store it from the cloud function to the firestore DB.
firebase.functions().httpsCallable("myFunction")({date: new Date()}) <---- // this doesn't work
So, I have decided to convert the Date object to a firestore timestamp in the client side before sending it to the cloud function. As follows:
date = firebase.firestore.Timestamp.fromDate(date);
firebase.functions().httpsCallable("myFunction")({date})
Then, if I get it in the cloud function and store it on my firestore, I see an object with the fields "seconds" and "nanoseconds", but not a timestamp (I mean it is not stored with the type Timestamp)... So when I get this date I cannot apply the method .toDate()
Is there any good way to store the date in this situation as a timestamp and not as an object?
The input and output of callable functions deal entirely with JSON, so they are limited to expressing only those types that are allowed in JSON: string, number, boolean, null, object, array. Date and Timestamp are not among those types. What the Firebase SDK will do is try to serialize custom objects as JSON, which is not working the way you want - the seconds and nanos fields of the Timestamp are being split up during serialization.
If you have a Date to send from the client, you should just send it as a number. Instead of passing a Date object, pass the single number inside it (milliseconds since unix epoch):
const number = date.getTime()
On the receiving side, in Cloud Functions, take the number and convert it back to a Date:
const date = new Date(number)
Then you can write the Date object to a Firestore document field.

get date from firebase format change

earlier we push date use native Firebase format
createAt:admin.firestore.Timestamp.fromDate(new Date)
so data store like this
"createAt": {
"_seconds": 1563181560,
"_nanoseconds": 567000000
}
new we change like this
createAt:new Date().toISOString()
now store like this
"createAt": "2019-07-17T07:17:05.115Z"
buts some date store like this because we use native date format
now how can i call old date to new date format? moste of the data sote like native date method is that any way to change new method?
The Firestore Timestamp type has many conversion methods. Just like it has a fromDate() it also has a toDate() method. So if you reload the data from Firestore and get a Timestamp object, you can then get a date with timestamp.toDate() and thus an ISO-8859 formatted string with timestamp.toDate().toISOString().

Querying and comparing Firestore serverTimestamp

I'm fairly new to Firebase/Firestore and I'm querying some stored data for analysis via Cloud Function.
I can't figure out how to compare timestamp within a query.
Each document has a field set using admin.firestore.FieldValue.serverTimestamp().
My initial query against the collection works, using ref.where("timestamp", ">=", startTime) where startTime is a Date object.
However, when I iterate through a snapshot - using doc.data().timestamp. Logging this value I get something like 2018-05-10T22:51:37.236Z which is not comparable.
Can I transform the doc.data().timestamp in a way that I can compare against a unix timestamp or Date object?

DATETIME in Javascript in Database

I'm trying to store a DATETIME in my Database using Javascript, there is just one problem, it doesn't store the DATETIME which was set to DATETIME in the database.
This is how I store and get the DATETIME,
var currentTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
query('UPDATE `users` SET `claimed`='+currentTime+' WHERE`id`='+pool.escape(user.steamid));
The problem is that the database doesn't update, but it can't be the fault of the database, did I miss something? Can you not store a Javascript "String" as Datetime?
Thanks.
Your Javascript varible is ok.
Try this
query('UPDATE `users` SET `claimed`="'+currentTime+'" WHERE`id`='+pool.escape(user.steamid));

Accessing item from LocalStorage (Angular 1.2)

I am running an old version of Angular - I have an object within my localStorage and I am trying to retrieve a single item from the object (rather than the whole the thing..)
var email = localStorage.user;
However the output of the localStorage.user variable is much longer (see below)- how would I retrieve the email address in this case 'bob#abc.com'?
{"firstname":"Bob","lastname":"Dole","id":"2000001999","accountId":"15","email":"bob#abc.com","instances":[{"name":"Test"}]}"
LocalStorage values are always stored as strings. In order to refer to the email in the object, we need to convert it to a JSON. We can do that by parsing the string using JSON.parse() method like below :
var email = JSON.parse(localstorage.user).email
You have Json structure not JS object, so parse it 1st
var email = JSON.parse(localStorage.user).email;
Using html5 local storage we can store data, add expire time by using cookies and sessions. https://stackoverflow.com/a/41385990/6554634 go through that, gives a lot of explanation

Categories

Resources