Created
May 17, 2020 14:15
-
-
Save peterkracik/c50f88c5715329c6f938cf47120650d9 to your computer and use it in GitHub Desktop.
Script to convert firebase timestamp to JS format date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static convertDate(firebaseObject: any) { | |
| if (!firebaseObject) return null; | |
| for (const [key, value] of Object.entries(firebaseObject)) { | |
| // covert items inside array | |
| if (value && Array.isArray(value) ) | |
| firebaseObject[key] = value.map(item => self.convertDate(item)); | |
| // convert inner objects | |
| if (value && typeof value === 'object' ){ | |
| firebaseObject[key] = self.convertDate(value); | |
| } | |
| // convert simple properties | |
| if (value && value.hasOwnProperty('seconds')) | |
| firebaseObject[key] = (value as firestore.Timestamp).toDate(); | |
| } | |
| return firebaseObject; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment