
public countDays(startDate: Date, endDate: Date): number {
console.log(typeof startDate);
const days = (endDate.getTime() - startDate.getTime()) / (1000 * 3600 * 24);
return Math.ceil(days);
}Absolutnie nie czaje... taki kod, na konsoli wypisuje mi "string"... czemu ? skoro parametr startDate jest typu Date ? już siwieje od tego ts... . Aplikacja ionic z angularem metodę wywołuje z template html.
#programowanie #angular #typescript
- Najlepsze
- Wszystkie komentarze








@DaxterPoker: Powinien być obiekt. To co trafia do funkcji nie jest instancją typu Date
Funkcję tak wywołuję:
this.countDays(new Date('1995-12-17T03:24:00'), new Date('1995-12-19T03:24:00'));{{ countDays(view.trip.startDate, view.trip.endDate )}} Days
Komentarz usunięty przez autora
Dokładnie tak samo zadziała jeżeli zamiast silnego typowania podasz ":any".
Problemem jest natomiast jaki jest rzeczywisty typ danych który jest przykazywany w "view.trip.startDate". Widać że jest to Sting, czyli problem masz na poziomie ustawiania tego pola ( ͡° ͜ʖ ͡°)
.ts :
tripform: FormGroup = new FormGroup({
tripId: new FormControl({value: undefined, disabled: false}, { nonNullable: true }),
name: new FormControl({value: '', disabled: false}, { nonNullable: true }),
budget: new FormControl({value: undefined,