Skip to content

Convert timestamps to javascript date object in the object fetched from firestore

License

Notifications You must be signed in to change notification settings

peterkracik/convert-firebase-timestamp

Repository files navigation

Convert firebase timestamps

Script that converts firebase timestamp values to the JS date format

  • converts each timestamp within a document (object), also inside its maps and array
  • converts single value
  • possible to call in rxjs pipe

How to use

document

 return this.db.collection('my-collection').doc(id).snapshotChanges().pipe(
    map(doc => convertTimestamps(doc.payload.data())
)

single value

return this.db.collection('my-collection').doc(id).snapshotChanges().pipe(
    map(doc => {
		const data = doc.payload.data();
		data.date = convertTimestamp(data.date);
		return data;
	})
)

in pipe ! don't call it before map, the original document is too large to be convert and it will throw recursion exception

return this.db.collection('my-collection').doc(id)
  .snapshotChanges().pipe(
        map(doc => doc.payload.data()),
        convertTimestampsPipe(),
    );
)

About

Convert timestamps to javascript date object in the object fetched from firestore

Resources

License

Stars

Watchers

Forks

Packages