3

I am trying to get readable date from Timestamp data type in my firestore database.

for (var ticketDoc of ticketsSnapshot.docs) {
            var timeStamp = await ticketDoc.data().TimePreferred;
            console.log(timeStamp.toDate());
            var time = new Date(timeStamp).toDate();
            ticketDoc.data().TimePreferred = time;
            tickets.push(ticketDoc.data());
        }

I read the question about a similar problem at :

How do I convert a Firestore date/Timestamp to a JS Date()?

so, i tried to do same and i expect the output of readable date, although it gives me the correct result in

console.log(timeStamp.toDate());

but also it gives me an error. Console output as follow :-

2019-04-10T06:30:00.000Z
TypeError: (intermediate value).toDate is not a function

Not : I am trying to get readable date in postman

  • 1
    Did you figure this out? I'm stuck with a similar problem https://stackoverflow.com/questions/61166977/rendering-a-firestore-timestamp-in-react – Mel Apr 12 '20 at 07:07

7 Answers7

1

Change the following line:

var time = new Date(timeStamp).toDate();

into this:

var time = new Date(timeStamp).toDateString();

From the docs:

A string representing the date portion of the given Date object in human readable form in American English.

Peter Haddad
  • 73,993
  • 25
  • 133
  • 124
1

I don't know why this timestamp object doesn't have the .toDate() extension, but if it has the 'seconds' and 'nanoseconds' properties, you can turn it to a JS Data with

Date(data.seconds)
Vagner Gon
  • 505
  • 8
  • 20
1

You can try in the following way

{{ formatDate(new Date(data.seconds*1000)) }}

You can use the format date function to display in desired format.

import moment from "moment";

  format_date(value) {
    if (value) {
      return moment(String(value)).format("DD/MM/YYYY");
    }
  },
srijan439
  • 241
  • 3
  • 7
0

Have you tried changing this to

var time = (new Date(timeStamp)).toDateString();
Sushant Somani
  • 1,368
  • 2
  • 12
  • 29
0

If the TimePreferred field in your document is a Timestamp, you can get a valid Date object from it by simply calling toDate() on it.

So:

for (var ticketDoc of ticketsSnapshot.docs) {
    var date = ticketDoc.data().TimePreferred.toDate();
}

None of these calls are asynchronous or returning a promise, so you don't need await.

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • 2
    Hey Frank, that works when you fetch the timestamp but when you're creating it and want to immediately display the value with .toDate() on the app it generates the `.toDate is not a function` multiple times until the data is ready – javebratt Jan 10 '20 at 15:59
0

From reasons that I don't know, it doesn't work at times, so a safer option would be to use the seconds and nanoseconds attributes found in the timestamp to convert it to date as follows:

const date = new Date(timestamp.seconds*1000 + timestamp.nanoseconds/100000) 
// construct the date from the absolute time in milliseconds

Note:

  • 1 second = 1000 ms
  • 1 nanosecond = 10^-6 ms
Srividya K
  • 695
  • 5
  • 19
0

You have to first make sure that the timestamp object is truly of type Timestamp. to do this, after you get the Timestamp from Firebase, create the Timestamp object:

const timestampObject: Timestamp = !!timeStamp
      ? new Timestamp(timeStamp.seconds, timeStamp.nanoseconds)
      : null;
me-and-viy
  • 41
  • 5