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
@DaxterPoker: To że kompilator pokazuje Ci że oczekiwany typ to Date czy String to ok, bo to są twoje założenia. 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 ( ͡°͜ʖ͡°)
@Mark9wi: @rossecki: nie no to dane które wpisuje w formularzu, i z tego formularza faktycznie wyciągam string jednak nie rzuca się gdy wkładam to do Date - co jest mega dziwne. Chciałem zrobić typowany formularz : .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, disabled: false}, {
Dawno nie pisałem formów ale raczej to że podajesz że dane pole ma być typu datetime nie oznacza że taki format tam dostajesz. Zawsze możesz dodać sobie dodatkowe parsowanie w metodzie. Jeżeli to string to try parse na datetime
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
@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'));
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, disabled: false}, {