Date object: A JavaScript object that represents a specific moment in time. Created using the new Date() constructor.
Note: The constructor can take year, month (0-11), and day parameters in that order.
- Use the new Date() constructor
- Pass year, month (0-indexed), and day as parameters
- Store the result in a variable
Use new Date(year, monthIndex, day) where monthIndex starts at 0.
Month 2 = March (0=January, 1=February, 2=March).
The variable now holds the date March 15, 2024.
// Creates March 15, 2024
let date = new Date(2024, 2, 15);
• Month indexing: Months are 0-indexed (0=January, 11=December)
• Parameter order: Year, Month, Day (Y-M-D)
• Constructor: Use new keyword to create Date objects
• Practice Tip: Remember: Month index = Calendar month - 1
- let birthday = new Date(2023, 11, 25); // December 25, 2023
- let today = new Date(2024, 0, 1); // January 1, 2024
- let halloween = new Date(2024, 9, 31); // October 31, 2024
- Months are 0-indexed: 0=January, 1=February, ..., 11=December
- Use Date constructor in Year-Month-Day order
- Remember: March = month index 2, not 3
Q: Why is the month index different from the calendar month?
A: JavaScript uses 0-based indexing for months, so January = 0, February = 1, etc.
Q: What if I use an invalid date?
A: JavaScript creates an "Invalid Date" object. Use isNaN() to check if a date is valid.
Date comparison: Using comparison operators (<, >, ===) to determine the relationship between two Date objects.
Note: Date objects can be compared directly using standard comparison operators.
- Create two Date objects
- Use comparison operators (<, >, ===) between them
- Store or use the boolean result
let date1 = new Date(2024, 2, 15);
let date2 = new Date(2024, 2, 20);
let isEarlier = date1 < date2;
Since March 15 is before March 20, the result is true.
let date2 = new Date(2024, 2, 20);
let isEarlier = date1 < date2; // Returns true
Yes, March 15, 2024 is earlier than March 20, 2024.
• Comparison operators: Use <, >, === to compare dates
• Boolean result: Comparison returns true or false
• Chronological order: Earlier dates are less than later dates
• Practice Tip: Test with known date relationships first
- new Date(2024, 0, 1) < new Date(2024, 11, 31) // true
- new Date(2024, 4, 15) > new Date(2024, 2, 10) // true
- new Date(2024, 3, 1) === new Date(2024, 3, 1) // true (use === for equality)
- Use < for "earlier than", > for "later than"
- For equality, use === (strict equality)
- Date objects compare based on their internal millisecond values
Q: What comparison operators can I use for dates?
A: You can use all comparison operators: <, >, <=, >=, ===, !==
Q: Does time matter in date comparison?
A: Yes! Date objects include time. new Date(2024, 2, 15, 10) < new Date(2024, 2, 15, 14) returns true.
Greater than (>): Operator that returns true if the left operand is later in time than the right operand.
Note: For dates, "greater than" means chronologically later.
- Create two Date objects
- Use the > operator between them
- Evaluate the boolean result
let date1 = new Date(2024, 5, 10); // June 10, 2024
let date2 = new Date(2024, 2, 15); // March 15, 2024
let isLater = date1 > date2;
Since June 10 is after March 15, the result is true.
let date2 = new Date(2024, 2, 15); // March 15
let isLater = date1 > date2; // Returns true
Yes, June 10, 2024 is greater than March 15, 2024.
• Greater than: > operator returns true if left date is later
• Chronological logic: Later dates are greater than earlier dates
• Boolean evaluation: Result is always true or false
• Practice Tip: Think of the number line for date ordering
- new Date(2024, 11, 31) > new Date(2024, 0, 1) // true
- new Date(2024, 7, 15) > new Date(2024, 7, 10) // true
- new Date(2024, 1, 1) > new Date(2024, 11, 31) // false
- > means "comes later in time"
- Think of dates on a timeline from left to right
- Any date in June > any date in March of the same year
Q: What if I compare dates in different years?
A: The year is considered first, then month, then day. new Date(2023, 11, 31) < new Date(2024, 0, 1) returns true.
Q: Can I chain multiple comparisons?
A: Yes! You can use expressions like date1 > date2 && date2 > date3.
Less than (<): Operator that returns true if the left operand is earlier in time than the right operand.
Note: For dates, "less than" means chronologically earlier.
- Create two Date objects
- Use the < operator between them
- Evaluate the boolean result
let date1 = new Date(2024, 0, 1); // January 1, 2024
let date2 = new Date(2024, 1, 14); // February 14, 2024
let isEarlier = date1 < date2;
Since January 1 is before February 14, the result is true.
let date2 = new Date(2024, 1, 14); // Feb 14
let isEarlier = date1 < date2; // Returns true
Yes, January 1, 2024 is less than February 14, 2024.
• Less than: < operator returns true if left date is earlier
• Chronological logic: Earlier dates are less than later dates
• Boolean evaluation: Result is always true or false
• Practice Tip: Remember: < means "earlier than"
- new Date(2024, 0, 1) < new Date(2024, 11, 31) // true
- new Date(2024, 4, 1) < new Date(2024, 4, 15) // true
- new Date(2024, 9, 31) < new Date(2024, 8, 15) // false
- < means "comes earlier in time"
- Think of the calendar year from January to December
- Any date in January < any date in February of the same year
Q: What happens if I compare the same dates?
A: new Date(2024, 4, 1) < new Date(2024, 4, 1) returns false since they're equal.
Q: Can I use <= for "less than or equal"?
A: Yes! new Date(2024, 4, 1) <= new Date(2024, 4, 1) returns true.
Equality (===): Operator that returns true if both operands represent the same date and time.
Note: For dates, equality means identical year, month, day, hour, minute, and second.
- Create two Date objects
- Use the === operator between them
- Evaluate the boolean result
let date1 = new Date(2024, 2, 15);
let date2 = new Date(2024, 2, 15);
let isEqual = date1 === date2;
Since both dates are identical, the result is true.
let date2 = new Date(2024, 2, 15);
let isEqual = date1 === date2; // Returns true
Yes, March 15, 2024 and March 15, 2024 are equal.
• Strict equality: === operator returns true if dates are identical
• Exact match: Year, month, day, and time must all match
• Boolean evaluation: Result is always true or false
• Practice Tip: Use === to check for specific dates
- new Date(2024, 11, 25) === new Date(2024, 11, 25) // true
- new Date(2024, 0, 1) === new Date(2023, 0, 1) // false
- new Date(2024, 6, 4) === new Date(2024, 6, 4) // true
- === checks for exact date and time match
- Year, month, day, and time must all be the same
- Use for validating specific dates like birthdays
Q: What if dates have different times?
A: Date objects include time. new Date(2024, 2, 15, 10) !== new Date(2024, 2, 15, 14) returns true.
Q: Can I check inequality with !==?
A: Yes! new Date(2024, 2, 15) !== new Date(2024, 2, 16) returns true.