Comparing Dates in TypeScript - Mathematics Grade 1 - Exercises with solutions

Master date comparisons in TypeScript: objects, methods, and comparisons through these 10 detailed exercises.

Solutions: Exercises 1 to 10
1 Basic date comparison
Exercise 1
How do I compare two dates in TypeScript to determine which is earlier?
Difficulty: Beginner Time: ~3 minutes Skills: Date Comparison Operators
Definition:

Date comparison: TypeScript uses comparison operators (<, >, =, <>) with Date objects to compare chronological order

Note: TypeScript compares dates by converting them to timestamps (milliseconds since epoch) internally.

Step-by-step comparison method:
  1. Create Date objects for the dates to compare
  2. Use comparison operators to evaluate relationships
  3. Understand that earlier dates have smaller timestamp values
Code
const date1 = new Date('2023-01-15');
const date2 = new Date('2023-01-20');
date1 < date2
Result
true
Step 1: Create Date objects

const date1 = new Date('2023-01-15'); const date2 = new Date('2023-01-20');

Step 2: Apply comparison operator

date1 < date2 evaluates if January 15 comes before January 20

Step 3: Determine chronological order

Since January 15 occurs before January 20, the result is true

date1 < date2 returns true
Final answer:

date1 < date2 = true

Applied rules:

• Date ordering: Earlier dates have smaller timestamp values

• Object comparison: TypeScript converts Date objects to timestamps for comparison

• Comparison operators: Same as for numbers but applied chronologically

• Practice Tip: Think of dates as positions on a timeline: left (earlier) < right (later)

Related Examples:
  • new Date('2023-02-01') > new Date('2023-01-31') - February comes after January
  • new Date('2023-03-10') === new Date('2023-03-10') - Same dates are equal
  • new Date('2022-12-31') < new Date('2023-01-01') - End of year before next year
Quick Tips:
  • TypeScript automatically handles leap years and month lengths in date comparisons
  • Always use Date objects for reliable comparison
  • Chronological thinking: earlier dates have "smaller" timestamp values than later dates
Frequently Asked Questions:

Q: Can I compare date strings directly?
A: It's better to use Date objects. String comparison follows alphabetical order, not chronological.

Q: What about time zones?
A: Date objects store UTC time internally, ensuring consistent comparisons regardless of local time zone.

2 Greater than operator
Exercise 2
How do I use the greater than operator to find dates that come after a specific date?
Difficulty: Beginner Time: ~4 minutes Skills: Date Comparison, Greater Than Operator
Definition:

Greater than operator: > returns true when the left date occurs after the right date chronologically

Note: This operator compares timestamps: later dates have higher timestamp values than earlier dates.

Step-by-step comparison method:
  1. Create Date objects for comparison
  2. Use the > operator to find dates occurring later
  3. Understand that higher timestamps correspond to later dates
Code
const date = new Date('2023-06-15');
const refDate = new Date('2023-06-01');
date > refDate
Result
true
Step 1: Create Date objects

const date = new Date('2023-06-15'); const refDate = new Date('2023-06-01');

Step 2: Apply the greater than operator

date > refDate finds if June 15 occurs after June 1

Step 3: Interpret the results

Since June 15 is after June 1, the result is true

date > refDate returns true
Final answer:

This comparison returns true for dates after June 1, 2023

Applied rules:

• Timestamp comparison: Later dates have higher timestamp values

• Chronological order: Higher timestamps mean later dates

• Practical use: Commonly used for finding recent records or future events

• Practice Tip: Remember: dates further right on timeline have higher values

Related Examples:
  • new Date('2023-12-26') > new Date('2023-12-25') - Day after Christmas
  • new Date('2023-03-01') > new Date('2023-02-28') - March after February
  • new Date('2024-01-01') > new Date('2023-12-31') - New year after previous
Quick Tips:
  • Use greater than for "after" conditions: date > new Date('2000-01-01') means after year 2000
  • Combine with other operators for ranges: date > start && date < end
  • Think of timeline: dates to the right have higher timestamp values
Frequently Asked Questions:

Q: What if I want dates on or after a specific date?
A: Use the greater than or equal operator: date >= new Date('2023-06-01')

Q: Can I use this with time components?
A: Yes, datetime comparisons work: new Date('2023-06-01T10:30') > new Date('2023-06-01T09:00')

3 Less than operator
Exercise 3
How do I use the less than operator to find dates that come before a specific date?
Difficulty: Beginner Time: ~4 minutes Skills: Date Comparison, Less Than Operator
Definition:

Less than operator: < returns true when the left date occurs before the right date chronologically

Note: This operator compares timestamps: earlier dates have lower timestamp values than later dates.

Step-by-step comparison method:
  1. Create Date objects for comparison
  2. Use the < operator to find dates occurring earlier
  3. Understand that lower timestamps correspond to earlier dates
Code
const date = new Date('2023-05-15');
const refDate = new Date('2023-06-01');
date < refDate
Result
true
Step 1: Create Date objects

const date = new Date('2023-05-15'); const refDate = new Date('2023-06-01');

Step 2: Apply the less than operator

date < refDate finds if May 15 occurs before June 1

Step 3: Interpret the results

Since May 15 is before June 1, the result is true

date < refDate returns true
Final answer:

This comparison returns true for dates before June 1, 2023

Applied rules:

• Timestamp comparison: Earlier dates have lower timestamp values

• Chronological order: Lower timestamps mean earlier dates

• Practical use: Commonly used for finding old records or past events

• Practice Tip: Remember: dates further left on timeline have lower values

Related Examples:
  • new Date('2022-12-31') < new Date('2023-01-01') - Last day of previous year
  • new Date('2023-02-14') < new Date('2023-02-28') - Valentine's before end of Feb
  • new Date('2023-01-01') < new Date('2023-12-31') - New Year before end of year
Quick Tips:
  • Use less than for "before" conditions: date < new Date('2010-01-01') means before 2010
  • Combine with other operators for ranges: date > start && date < end
  • Think of timeline: dates to the left have lower timestamp values
Frequently Asked Questions:

Q: What if I want dates on or before a specific date?
A: Use the less than or equal operator: date <= new Date('2023-06-01')

Q: Can I use this with time components?
A: Yes, datetime comparisons work: new Date('2023-06-01T08:00') < new Date('2023-06-01T09:30')

Solutions: Exercises 4 to 5
4 Equal operator
Exercise 4
How do I find dates that match a specific date exactly?
Definition:

Strict equality operator: === returns true only when both Date objects have identical timestamps

Note: For Date objects, use getTime() to compare timestamps, as === compares references, not values.

Step-by-step comparison method:
  1. Create Date objects for comparison
  2. Use getTime() method to compare timestamps
  3. Structure comparison to return boolean result
Code
const date1 = new Date('2023-06-01');
const date2 = new Date('2023-06-01');
date1.getTime() === date2.getTime()
Result
true
Step 1: Create Date objects

const date1 = new Date('2023-06-01'); const date2 = new Date('2023-06-01');

Step 2: Use getTime() for value comparison

date1.getTime() === date2.getTime() compares actual date values

Step 3: Interpret the results

Since both dates are identical, the result is true

date1.getTime() === date2.getTime() returns true
Final answer:

This comparison returns true only for identical dates

Applied rules:

• Value comparison: Use getTime() to compare actual date values

• Reference vs value: === compares references, not values for objects

• Practical use: Commonly used for finding specific events or appointments

• Practice Tip: Remember: date1 === date2 rarely true for different objects

Related Examples:
  • new Date('2023-01-01').getTime() === new Date('2023-01-01').getTime() - New Year's Day
  • new Date('2023-12-25').getTime() === new Date('2023-12-25').getTime() - Christmas Day
  • new Date('2023-07-04').getTime() === new Date('2023-07-04').getTime() - Independence Day
Quick Tips:
  • Use getTime() for actual date value comparison, not ===
  • Combine with other conditions: date.getTime() === target.getTime() && status === 'active'
  • Date objects with same date/time have same timestamp value
Frequently Asked Questions:

Q: Why doesn't date1 === date2 work for Date objects?
A: Because === compares object references, not values. Two different Date objects are different references even if they represent the same date.

Q: What about datetime values?
A: Same principle: use getTime() to compare the actual timestamp values.

5 Not equal operator
Exercise 5
How do I find dates that do NOT match a specific date?
Definition:

Not equal operator: !== or != returns true when dates have different timestamps

Note: Use getTime() to properly compare date values for inequality.

Step-by-step comparison method:
  1. Create Date objects for comparison
  2. Use getTime() to compare timestamps
  3. Apply !== operator to find non-matching dates
Code
const date = new Date('2023-06-15');
const refDate = new Date('2023-06-01');
date.getTime() !== refDate.getTime()
Result
true
Step 1: Create Date objects

const date = new Date('2023-06-15'); const refDate = new Date('2023-06-01');

Step 2: Use getTime() for value comparison

date.getTime() !== refDate.getTime() checks if timestamps are different

Step 3: Interpret the results

Since June 15 and June 1 are different dates, the result is true

date.getTime() !== refDate.getTime() returns true
Final answer:

This comparison returns true for dates different from June 1, 2023

Applied rules:

• Value comparison: Use getTime() to compare actual date values

• Exclusion: Only different timestamps return true

• Practical use: Commonly used for excluding specific dates

• Practice Tip: Remember: !== and != work similarly with Date objects

Related Examples:
  • new Date('2023-01-02').getTime() !== new Date('2023-01-01').getTime() - Jan 2 vs Jan 1
  • new Date('2023-12-24').getTime() !== new Date('2023-12-25').getTime() - Christmas Eve vs Day
  • new Date('2023-07-05').getTime() !== new Date('2023-07-04').getTime() - Day after Independence
Quick Tips:
  • Use getTime() for accurate value comparison: date.getTime() !== target.getTime()
  • Combine with other operators: date.getTime() !== holiday.getTime() && date.getTime() < end.getTime()
  • Both !== and != work with getTime() for value comparison
Frequently Asked Questions:

Q: What's the difference between !== and != with dates?
A: Both work the same way when using getTime(), but !== is preferred for strict comparison.

Q: How does undefined/null affect comparisons?
A: null.getTime() will cause an error. Always validate date objects before comparison.

Solutions: Exercises 6 to 10
6 Compare method
Exercise 6
How do I use a custom compare function to sort dates?
Definition:

Compare function: A function that returns negative, zero, or positive values to indicate ordering relationship between two dates

Note: Custom compare functions are essential for sorting arrays of Date objects.

Step-by-step comparison method:
  1. Define a compare function that takes two Date parameters
  2. Return the difference of their timestamps
  3. Use the function with array.sort()
Code
dates.sort((a, b) => a.getTime() - b.getTime())
Before Sort
[Jun 15, Mar 10, Dec 25]
After Sort
[Mar 10, Jun 15, Dec 25]
Step 1: Define the compare function

(a, b) => a.getTime() - b.getTime() calculates the difference between timestamps

Step 2: Apply to array sorting

dates.sort((a, b) => a.getTime() - b.getTime()) sorts chronologically

Step 3: Understand the return values

Positive: a > b; Negative: a < b; Zero: a === b

Array sorted in chronological order
Final answer:

Array sorted chronologically: earliest to latest dates

Applied rules:

• Compare function: Returns difference of timestamps for chronological sorting

• Sorting: Positive value means first element comes after second

• Practical use: Essential for organizing date-based data

• Practice Tip: Use getTime() to get numeric timestamp values

Related Examples:
  • dates.sort((a, b) => b.getTime() - a.getTime()) - Reverse chronological (latest first)
  • events.sort((a, b) => a.getTime() - b.getTime()) - Sort events chronologically
  • appointments.sort((a, b) => b.getTime() - a.getTime()) - Latest appointments first
Quick Tips:
  • For ascending order (earliest first): a.getTime() - b.getTime()
  • For descending order (latest first): b.getTime() - a.getTime()
  • Compare functions work with any array method requiring ordering
Frequently Asked Questions:

Q: Why can't I just use dates.sort() without a compare function?
A: Because Date objects are compared as strings by default, not chronologically.

Q: Can I sort in reverse chronological order?
A: Yes! Use b.getTime() - a.getTime() to sort latest dates first.

7 getTime method
Exercise 7
How do I get the numeric timestamp value of a date?
Definition:

getTime method: date.getTime() returns the number of milliseconds since January 1, 1970 (Unix epoch)

Note: This numeric value enables mathematical operations and reliable comparisons between dates.

Step-by-step extraction method:
  1. Create a Date object
  2. Call the getTime() method on the date
  3. Use the numeric timestamp for calculations
Code
const date = new Date('2023-06-15');
date.getTime()
Result
1686787200000
Step 1: Create a Date object

const date = new Date('2023-06-15') creates a date object

Step 2: Call getTime() method

date.getTime() returns the timestamp in milliseconds

Step 3: Use the numeric value

The result (1686787200000) can be used for calculations

Step 4: Compare with other dates

Compare timestamps directly: date1.getTime() < date2.getTime()

date.getTime() returns 1686787200000 (timestamp in milliseconds)
Final answer:

getTime() provides the numeric timestamp value for mathematical operations

Applied rules:

• Timestamp conversion: Convert Date objects to numeric values

• Mathematical operations: Use timestamps for date arithmetic

• Reliable comparison: Numeric comparison is more reliable than object comparison

• Practice Tip: getTime() is essential for accurate date comparisons

Related Examples:
  • new Date('2023-01-01').getTime() = 1672531200000 - New Year timestamp
  • new Date('2023-12-31').getTime() = 1703980800000 - Year end timestamp
  • const diff = date2.getTime() - date1.getTime() - Calculate difference in ms
Quick Tips:
  • Use getTime() for accurate date comparisons: date1.getTime() < date2.getTime()
  • Timestamps enable mathematical operations on dates
  • getTime() is crucial for sorting Date objects in arrays
Frequently Asked Questions:

Q: What is the significance of the large number returned?
A: It's milliseconds since January 1, 1970 (Unix epoch). Each day = 86,400,000 milliseconds.

Q: Can I convert a timestamp back to a Date?
A: Yes! new Date(timestamp) creates a Date object from a timestamp.

8 Date ranges
Exercise 8
How do I check if a date falls within a specific range?
Definition:

Date range: Using logical operators to check if a date is between two boundary dates inclusively

Note: Range checking requires both lower and upper boundary comparisons.

Step-by-step range method:
  1. Create Date objects for the target date and range boundaries
  2. Use comparison operators to check both boundaries
  3. Combine conditions with logical AND operator
Code
const date = new Date('2023-06-15');
const start = new Date('2023-06-01');
const end = new Date('2023-06-30');
date >= start && date <= end
Result
true
Step 1: Create Date objects

const date = new Date('2023-06-15'); const start = new Date('2023-06-01'); const end = new Date('2023-06-30');

Step 2: Check lower boundary

date >= start checks if date is on or after the start date

Step 3: Check upper boundary

date <= end checks if date is on or before the end date

Step 4: Combine with logical AND

date >= start && date <= end ensures both conditions are met

date >= start && date <= end returns true
Final answer:

This returns true if date is within the inclusive range [start, end]

Applied rules:

• Inclusive range: Both boundary dates are included in the check

• Logical operators: Use && to ensure both conditions are satisfied

• Practical use: Commonly used for filtering date-based data

• Practice Tip: Always check both boundaries separately then combine

Related Examples:
  • date >= new Date('2023-01-01') && date <= new Date('2023-12-31') - Full year 2023
  • date >= new Date('2023-03-01') && date <= new Date('2023-05-31') - First quarter
  • date >= new Date('2023-07-04') && date <= new Date('2023-09-04') - Summer period
Quick Tips:
  • Range checks include both boundary dates with >= and <=
  • Always verify the order: start date should be before end date
  • Combine with other conditions: dateInRange && status === 'active'
Frequently Asked Questions:

Q: What happens if I reverse the start and end dates?
A: The condition will always be false since no date can be simultaneously less than the first date and greater than the second.

Q: Can I use this with datetime values?
A: Yes! The same logic works with datetime objects: date >= startDateTime && date <= endDateTime

9 Time components
Exercise 9
How do I compare dates that include time components?
Definition:

DateTime comparison: Comparing Date objects that include both date and time components

Note: DateTime comparisons consider the full timestamp, including hours, minutes, seconds, and milliseconds.

Step-by-step datetime comparison:
  1. Create Date objects with time components
  2. Use comparison operators for chronological comparison
  3. Understand that time components affect the ordering
Code
const date1 = new Date('2023-06-15T09:00:00');
const date2 = new Date('2023-06-15T14:30:00');
date1 < date2
Result
true
Step 1: Create Date objects with time

const date1 = new Date('2023-06-15T09:00:00'); const date2 = new Date('2023-06-15T14:30:00');

Step 2: Apply comparison operator

date1 < date2 compares both date and time components

Step 3: Consider time components

Even though dates are the same, 9:00 AM comes before 2:30 PM

Step 4: Get the result

Since 9:00 AM on June 15 is before 2:30 PM on June 15, the result is true

date1 < date2 returns true (9:00 AM before 2:30 PM)
Final answer:

DateTime comparisons consider both date and time components

Applied rules:

• Full timestamp: DateTime comparisons include hours, minutes, seconds, milliseconds

• Chronological order: Earlier times on same date are "less than" later times

• Practical use: Essential for scheduling, appointments, event ordering

• Practice Tip: Remember that time components can affect same-date comparisons

Related Examples:
  • new Date('2023-01-01T23:59:59') < new Date('2023-01-02T00:00:00') - End of day before next
  • new Date('2023-06-15T08:00:00') < new Date('2023-06-15T17:00:00') - Morning before evening
  • new Date('2023-12-31T12:00:00') < new Date('2024-01-01T00:00:00') - Noon before midnight
Quick Tips:
  • DateTime comparisons consider the complete timestamp including time
  • Same date, different times: earlier times are "less than" later times
  • ISO format (YYYY-MM-DDTHH:mm:ss) is recommended for consistency
Frequently Asked Questions:

Q: How precise are datetime comparisons?
A: They consider milliseconds: new Date('2023-06-15T09:00:00.001') > new Date('2023-06-15T09:00:00.000')

Q: What about time zones?
A: Date objects store UTC time internally, ensuring consistent comparisons regardless of local time zone.

10 Complex date comparisons
Exercise 10
How do I combine multiple date comparison conditions in one expression?
Definition:

Complex date comparisons: Combining multiple date conditions using logical operators (&&, ||, !)

Note: Complex expressions allow sophisticated filtering based on multiple date criteria.

Step-by-step complex comparison:
  1. Identify all date conditions needed
  2. Use logical operators to combine conditions
  3. Apply parentheses to control precedence
  4. Test the expression logic carefully
Code
const date = new Date('2023-06-15');
date >= new Date('2023-06-01') && date <= new Date('2023-06-30') && date.getTime() !== new Date('2023-06-04').getTime()
Condition 1
>= June 1, 2023
Condition 2
<= June 30, 2023
Condition 3
!= June 4, 2023
Step 1: Identify the range condition

Check if date is in June: date >= new Date('2023-06-01') && date <= new Date('2023-06-30')

Step 2: Add the exclusion condition

Exclude June 4: date.getTime() !== new Date('2023-06-04').getTime()

Step 3: Combine with AND operator

All conditions must be true for the expression to return true

Step 4: Interpret the result

Returns true for June dates except June 4, 2023

Expression returns true for June dates except June 4, 2023
Final answer:

Combines range selection with exclusion: June dates except June 4

Applied rules:

• Logical combinations: && (AND), || (OR), ! (NOT) to connect conditions

• Precedence: Use parentheses to control evaluation order

• Complex filtering: Sophisticated date-based expressions

• Practice Tip: Test each condition separately before combining

Related Examples:
  • date > new Date('2023-01-01') && date < new Date('2023-12-31') - All dates in 2023
  • date.getTime() === new Date('2023-07-04').getTime() || date.getTime() === new Date('2023-12-25').getTime() - Specific holidays
  • date >= new Date('2023-01-01') && date <= new Date('2023-03-31') && date.getTime() !== new Date('2023-02-14').getTime() - Q1 except Valentine's
Quick Tips:
  • Use parentheses to group related conditions: (date > start && date < end)
  • && means all conditions must be true; || means any condition can be true
  • Test complex expressions with simple cases first
Frequently Asked Questions:

Q: What's the order of operations for logical operators?
A: ! (NOT) is evaluated first, then && (AND), then || (OR). Use parentheses to override.

Q: How do I debug complex date expressions?
A: Break them into smaller parts and test each condition separately.

Key Laws, Methods, Rules, and Definitions
date1 < date2
Date Comparison Operator
Key definitions:

Date comparison: Evaluating temporal relationships between dates using operators (<, >, ===, etc.)

Timestamp: Numeric value representing milliseconds since Unix epoch (January 1, 1970)

Chronological order: Earlier dates have "less than" relationship with later dates

Complete methodology:
  1. Analyze the date requirement: Identify what temporal relationship to evaluate
  2. Choose the method: Select appropriate comparison operator or method (.getTime())
  3. Apply the method: Execute the date comparison or function
  4. Verify the result: Check that the comparison makes chronological sense
Tip 1: Always use getTime() for accurate Date object comparisons, not ===.
Tip 2: Think of dates as positions on a timeline: left (earlier) < right (later).
Tip 3: Range checks use both >= and <= for inclusive boundaries.
Tip 4: DateTime comparisons include both date and time components in ordering.
Common errors: Using === for Date objects, ignoring time components, mixing date formats, incorrect operator usage.
Exam preparation: Memorize comparison operators, understand getTime() for value comparison, practice range checks, verify chronological logic.
Methods to memorize:

• Comparison operators: < (less than), > (greater than), === (strict equality), !== (not equal)

• Value comparison: date.getTime() === otherDate.getTime() for actual date values

• Range checks: date >= start && date <= end (inclusive on both ends)

• Sorting: dates.sort((a, b) => a.getTime() - b.getTime()) for chronological order

• Logical operators: && (AND), || (OR), ! (NOT) to combine conditions

Rules and Methods for Date Comparisons in TypeScript
date1 < date2
Less Than Date Comparison
Method 1
date1 < date2
Date 1 before Date 2
Method 2
date1.getTime() === date2.getTime()
Dates have same timestamp
Method 3
date >= start && date <= end
Date within range (inclusive)

Key Takeaways

  • Date comparisons use standard operators but with chronological meaning
  • Use getTime() for actual date value comparison, not ===
  • Earlier dates are considered "less than" later dates on timeline
  • Range checks include both boundary dates with >= and <=
  • DateTime comparisons consider time components in ordering
  • Combine conditions with logical operators for complex expressions

Questions & Answers

Question: I don't understand why new Date('2023-01-01') < new Date('2023-01-02') is true. Isn't 2 bigger than 1?

Answer: Great question! The key is understanding that date comparisons work differently than regular number comparisons:

  • With dates, we compare chronologically (by time order), not numerically
  • January 1, 2023 comes BEFORE January 2, 2023 on the calendar
  • In TypeScript, earlier dates are considered "less than" later dates

Think of it like a timeline: January 1 is to the left of January 2, so it has a "smaller" value in chronological terms.

Example: On a timeline, new Date('2023-01-01') is positioned to the left of new Date('2023-01-02'), so the comparison returns true.

Question: When I compare dates, why doesn't date1 === date2 work even when they represent the same date?

Answer: This is because === compares object references, not values:

  • Two different Date objects are different references even if they represent the same date
  • Each new Date() creates a unique object in memory
  • To compare actual date values, use the getTime() method

Example: const d1 = new Date('2023-06-01'); const d2 = new Date('2023-06-01'); d1 === d2 is false, but d1.getTime() === d2.getTime() is true.

Tip: Always use getTime() when comparing Date object values!

Question: What's the difference between < and <= when comparing dates?

Answer: The difference is about inclusivity:

  • < means "strictly less than" - date must come BEFORE another date
  • <= means "less than or equal to" - date can be BEFORE or THE SAME AS another date
  • For date ranges, <= includes the boundary date, < excludes it

Example: If today is June 15:

  • today < new Date('2023-06-15') is false (same date, not less)
  • today <= new Date('2023-06-15') is true (same date is acceptable)

Use <= when you want to include the boundary date in your condition!

Comprehensive Summary: Comparing Dates in TypeScript

Core Concepts:

Date Objects: In TypeScript, dates are represented as Date objects that store both date and time information as timestamps (milliseconds since Unix epoch).

Timestamp Values: Each Date object has a numeric timestamp value accessible via the getTime() method, which is crucial for accurate comparisons.

Chronological Ordering: Dates are ordered chronologically where earlier dates have smaller timestamp values than later dates.

Core Methods and Rules:
  1. Basic Comparison: Use operators <, >, <=, >= to compare Date objects chronologically
  2. Equality Comparison: Use getTime() method for value comparison: date1.getTime() === date2.getTime()
  3. Range Checking: Combine conditions with logical AND: date >= start && date <= end
  4. Array Sorting: Use compare functions with getTime(): dates.sort((a, b) => a.getTime() - b.getTime())
Progressive Examples:

Simple Comparison: new Date('2023-01-01') < new Date('2023-01-02') // true

Equality Check: new Date('2023-01-01').getTime() === new Date('2023-01-01').getTime() // true

Range Check: date >= new Date('2023-06-01') && date <= new Date('2023-06-30') // June dates

Advanced Comparison: date > new Date('2023-01-01') && date < new Date('2023-12-31') && date.getDay() !== 0 // Working days in 2023

Critical Tips and Pitfalls:
  • Never use === for Date objects: Use getTime() for value comparison instead
  • Time components matter: DateTime comparisons include hours, minutes, seconds
  • Use ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss for consistent parsing
  • Validate before comparison: Check that Date objects are valid before comparing
Key Notes for Memorization:

• Date objects store timestamps internally and compare chronologically

• Always use getTime() when comparing Date object values

• Range checks use both >= and <= for inclusive boundaries

• DateTime comparisons consider the complete timestamp including time components

• Combine conditions with logical operators for complex requirements

Date Functions Glossary

Date Object
A built-in TypeScript object that represents a specific moment in time, storing both date and time information as a timestamp.
Timestamp
A numeric value representing the number of milliseconds since January 1, 1970 (Unix epoch), used internally by Date objects.
getTime() Method
A method that returns the numeric timestamp value of a Date object, enabling accurate value comparisons.
Chronological Order
The ordering of dates based on their occurrence in time, where earlier dates are "less than" later dates.
Date Range
A pair of boundary dates that define an inclusive interval, checked using comparison operators and logical AND.

Elementary Data Education Team

Certified Elementary Educators & Data Analysis Specialists

Our team of experienced elementary school teachers and data analysis specialists creates age-appropriate, conceptually clear resources for introducing programming concepts to young learners. All content is aligned with elementary mathematics standards and reviewed by educational technology experts to ensure accessibility and pedagogical effectiveness.