Skip to content
Advertisement

How to print Firestore timestamp as formatted date and time like December 28,2020 at 3:52:04 AM UTC+5:30?

while fetching date from firestore I am getting Timestamp “seconds: 1608490949, nanoseconds: 275000000”.I want to print it as properly formatted date and time. As it is “December 28,2020 at 3:52:04 AM UTC+5:30”. Below is my pic of code

    obj.modify=this.dateconversion(obj.modify);
    dateconversion( time:Timestamp){
      return time.toDate();
    }
    
    It is returning  me values "2020-12-27T22:22:04.000Z" but actual in firestore "December 28,2020 at 
    3:52:04 AM UTC+5:30".
    Its seems giving me 5 hours back that's why one day back data is printing.
    Can any one please suggest other way to do or where I am doing mistake.

Advertisement

Answer

You can call javascript date functions here because this converting into javascript date

dateconversion( time:Timestamp){
  return time.toDate().toString();
  
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString

Or for more control on formatting, you can use momentjs [https://momentjs.com/docs/]

Advertisement