isFuture
Checks if the given date is in the future.
1/**
2 * Checks if the given date is in the future.
3 *
4 * @param date - The date to check.
5 * @returns True if the date is after the current time, false otherwise.
6 */
7export function isFuture(date: Date): boolean {
8 return date.getTime() > Date.now();
9}
Minimal Overhead
Leverages direct timestamp comparison for fast execution with no intermediate calculations.
Clear and Self-Documenting
The function name directly reflects its behavior, making the code highly readable and intention-revealing.
Pure and Side-Effect Free
Stateless implementation ensures safe reuse and predictable behavior.
Tests | Examples
1test('isFuture - returns true for a date in the future', () => {
2 const future = new Date(Date.now() + 10000); // 10 seconds from now
3 expect(isFuture(future)).toBe(true);
4});
5
6test('isFuture - returns false for a date in the past', () => {
7 const past = new Date(Date.now() - 10000); // 10 seconds ago
8 expect(isFuture(past)).toBe(false);
9});
10
11test('isFuture - returns false for the current moment', () => {
12 const now = new Date();
13 // Small margin due to execution time
14 expect(isFuture(now)).toBe(false);
15});
Common Use Cases
Scheduling Validation
Verify that a selected date for a meeting, delivery, or event lies in the future.
Countdown Timers
Determine whether to start or display a countdown for upcoming deadlines or launches.
Feature Availability Checks
Enable or disable functionality based on whether a feature’s activation date has arrived.
Notification Systems
Filter upcoming reminders or alerts based on future timestamps.