0

I have an app with a calendar, I want to add events to this calendar by fetching data from firebase on events and passing the dates through.

I have made a sample collection called 'competition' with a date in timestamp format.

When i console.log(), I can see the timestamp which is called datecomp is being passed through in Epoch seconds as below datecomp: t {seconds: 1585396800, nanoseconds: 0}

How can I convert this in Angular to get in the format of a date which can be used in the calendar?

The basic code I have so far is here for reference

Any help would be appreciated

TypeScript:

import { Component } from ‘@angular/core’;
import {tap} from ‘rxjs/operators’;
import {AngularFirestoreModule, AngularFirestore} from ‘@angular/fire/firestore’;
import {Injectable} from ‘@angular/core’;

@Component({
selector: ‘app-home’,
templateUrl: ‘home.page.html’,
styleUrls: [‘home.page.scss’],
})
export class HomePage {
date: string;
type: ‘string’;

constructor(private afs:AngularFirestore) {
this.afs.collection(‘competition’).valueChanges().subscribe(data=>{console.log(data);
})
}

HTML


Calendar Page
<ion-calendar [(ngModel)]="date"
  (onChange)="onChange($event)"
  [type]="type"
  [format]="'YYYY-MM-DD'">
</ion-calendar>
Ryan Kelly
  • 11
  • 1
  • 2
  • you can try converting utc seconds to date using date.setUTCSeconds(). Refer https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date. – Ravi Teja Vattem Apr 26 '20 at 14:23
  • Calling `toDate()` on the field value should do the trick. See https://stackoverflow.com/questions/52247445/how-do-i-convert-a-firestore-date-timestamp-to-a-js-date – Frank van Puffelen Apr 26 '20 at 14:27
  • @FrankvanPuffelen so in the code how would I do this? Do i need to call the firebase timestamp field? Sorry I am new to typescript and firebase – Ryan Kelly Apr 26 '20 at 15:52
  • You'll have to find the date field in your document, and then call `toDate()` on it. – Frank van Puffelen Apr 26 '20 at 16:06
  • @frankvanpuffelen okay the date field in my document is called datecomp so to get it into the format I need to use something along the lines of const t = firebase.firestore.Timestamp.datecomp(new Date()); const d = t.toDate(); Is this correct? sorry for keep asking its just a project i really need to get finished in the next couple of days and this issue is hindering me – Ryan Kelly Apr 27 '20 at 11:19

0 Answers0