6

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
  • In some cases you have to create the Timestamp object before being able to convert it: `new firebase.firestore.Timestamp(timestamp._seconds, timestamp._nanoseconds)` – Gesichtsfelsen Aug 18 '23 at 12:33

9 Answers9

2

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
  • 78,874
  • 25
  • 140
  • 134
2

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
  • 595
  • 9
  • 23
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
  • 401
  • 5
  • 7
1

For 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
  • 755
  • 6
  • 24
0

Have you tried changing this to

var time = (new Date(timeStamp)).toDateString();
Sushant Somani
  • 1,450
  • 3
  • 13
  • 31
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
  • 565,676
  • 79
  • 828
  • 807
  • 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

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
  • 51
  • 8
0

For Angular

import { Location, DatePipe } from '@angular/common';

constructor(
    public datepipe: DatePipe
  ) { }

const dayAsTimestamp = your_Timestamp_value;
const dayAsDate = new Date(dayAsTimestamp.seconds * 1000);
const dayAsString = this.datepipe.transform(dayAsDate, 'dd-MMM-yyyy');
Włodzimierz Woźniak
  • 3,106
  • 1
  • 26
  • 23
0

the possible reason toDate is not a function is you store the data in browser local storage which serialize object to JSON

since function cannot be serialized, so you are left with seconds and nanoseconds properties only

Acid Coder
  • 2,047
  • 15
  • 21