Skip to content

Instantly share code, notes, and snippets.

@peterkracik
Created May 17, 2020 14:15
Show Gist options
  • Select an option

  • Save peterkracik/c50f88c5715329c6f938cf47120650d9 to your computer and use it in GitHub Desktop.

Select an option

Save peterkracik/c50f88c5715329c6f938cf47120650d9 to your computer and use it in GitHub Desktop.
Script to convert firebase timestamp to JS format date
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