I want to write a condition that the day of the flight that someone choose is equal/ greatet than today:
heres the service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class FlightSelectionService {
startingCity: string = 'Warsaw';
selectedEndingCity: string = 'Paris';
startingDate: Date = new Date();
endingDate: Date = this.startingDate;
currentDate: Date = new Date();
adultsNumber: number = 0;
childrenNumber: number = 0;
validatePassengers(): boolean {
const totalPassengers = this.adultsNumber + this.childrenNumber;
return totalPassengers > 0;
}
validateDates(): boolean {
console.log(this.startingDate + ' ' + this.startingDate.getTime());
console.log(this.currentDate + ' ' + this.currentDate.getTime());
console.log(this.startingDate.getTime() === this.currentDate.getTime());
const isStartingDateValid =
this.startingDate.getTime() >= this.currentDate.getTime();
const isEndingDateValid = this.endingDate > this.startingDate;
const areDatesDifferent =
this.startingDate.getTime() !== this.endingDate.getTime();
return isStartingDateValid && isEndingDateValid && areDatesDifferent;
}
constructor() {}
}
and the validation code inside valide Date function:
console.log(this.startingDate + ' ' + this.startingDate.getTime());
console.log(this.currentDate + ' ' + this.currentDate.getTime());
console.log(this.startingDate.getTime() === this.currentDate.getTime());
const isStartingDateValid =
this.startingDate.getTime() >= this.currentDate.getTime();
it doesnt work, and i tried:
const startingDateInUTC = new Date(this.startingDate.getUTCFullYear(), this.startingDate.getUTCMonth(), this.startingDate.getUTCDate());
const currentDateInUTC = new Date(this.currentDate.getUTCFullYear(), this.currentDate.getUTCMonth(), this.currentDate.getUTCDate());
const isStartingDateValid = startingDateInUTC.getTime() >= currentDateInUTC.getTime();
How to implement this date comparsion correctly? The startingDate has to be currentDate or higher, so i can book flight for day or the future, not for the past.