1

I am taking the firebase server time and I want to convert that time to relative time(X seconds ago)

firebase.firestore.FieldValue.serverTimestamp()

I tried to convert the date time using momentjs but got "invalid date"

moment(date).fromNow()

I am taking the date from firebase because I want the exact real date time (not the device date time)

Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
Syed Rajin
  • 61
  • 1
  • 9

2 Answers2

4

It's probably a duplicate of this.

Write date using serverTimestamp() on Firestore.
Read date using createdAt.toDate() to receive a Javascript Date object.
Moment date using with moment(createdAt.toDate()).
Moment relative : moment(createdAt.toDate()).fromNow()

Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
0

You can convert a firebase server date to milliseconds using fromMillis function and then compare(subtract) it with the current date-time stamp(in milliseconds). Then divide the resultant milliseconds with 60000(1min) to show time lapsed in mins and with 60*60000 to show time lapsed in hours and similarly for days.

Gaurav Rana
  • 41
  • 1
  • 3