Firebase TIMESTAMP to date and Time

后端 未结 18 2552
温柔的废话
温柔的废话 2020-11-27 03:39

I am using firebase for my chat application. In chat object I am adding time stamp using Firebase.ServerValue.TIMESTAMP method.

I need to show the messa

18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 03:44

    Here is a safe method to convert a value from firebase Timestamp type to JS Date in case the value is not Timestamp the method returns it as it is

    Works for Angular 7/8/9

    import firebase from 'firebase';
    import Timestamp = firebase.firestore.Timestamp;
    
    export function convertTimestampToDate(timestamp: Timestamp | any): Date | any {
      return timestamp instanceof Timestamp
        ? new Timestamp(timestamp.seconds, timestamp.nanoseconds).toDate()
        : timestamp;
    }
    

提交回复
热议问题