Reputation: 81
I wanted the display the records only after 5 mins from the current timestamp.
Below is my HTML Code,
<div *ngFor="let bookings of newBookings" class="card mb-4">
<div *ngIf="isTimestampAfterCurrent(bookings?.currentTimeStamp)" class="card-body">
....
</div>
</div>
In my component.ts
isTimestampAfterCurrent(timestamp: string): boolean {
if (!timestamp) {
// If timestamp is not available, display the booking
return true;
}
const currentTimestamp = moment(); // Get the current timestamp
const bookingTimestamp = moment(timestamp, 'YYYY-MM-DD HH:mm:ss');
console.log(bookingTimestamp);
return bookingTimestamp.isAfter(currentTimestamp.add(5, 'minutes'));
}
Note: Format of the currentTimeStamp = 2023-06-25 02:29:49
Upvotes: 0
Views: 74
Reputation: 11
The problem is in the last line of your code. The .add()
function only mutates the Moment
object but doesn't seem to return it.
Here is a working example:
const currentMoment = moment('2023-01-01'); // Current Moment
const futureMoment4 = moment('2023-01-01').add(4, 'minutes'); // Moment 4 Minutes from Now
const futureMoment10 = moment('2023-01-01').add(10, 'minutes'); // Moment 10 Minutes from Now
currentMoment.add(5, 'm');
const isFutureMoment4Ahead = futureMoment4.isAfter(currentMoment);
const isFutureMoment10Ahead = futureMoment10.isAfter(currentMoment);
if (isFutureMoment4Ahead) {
console.log("futureMoment4 is more than 5 minutes ahead of currentMoment");
} else {
console.log("futureMoment4 is not more than 5 minutes ahead of currentMoment"); // this is the output
}
if (isFutureMoment10Ahead) {
console.log("futureMoment10 is more than 5 minutes ahead of currentMoment"); // this is the output
} else {
console.log("futureMoment10 is not more than 5 minutes ahead of currentMoment");
}
Upvotes: 0