JavaScript Dates

JavaScript Dates
JavaScript provides a built-in object called Date to work with dates and time. You can create, format, compare, and manipulate dates using the Date() object.
Creating a Date Object
1. Current Date and Time
2. Specific Date
3. Using Year, Month, Day Format
(Month starts from 0: January = 0, February = 1, …)
Date Methods
| Method | Description | Example |
|---|---|---|
getFullYear() | Returns year | date.getFullYear() |
getMonth() | Returns month (0–11) | date.getMonth() |
getDate() | Day of month (1–31) | date.getDate() |
getDay() | Weekday (0–6) | date.getDay() |
getHours() | Hours (0–23) | date.getHours() |
getMinutes() | Minutes | date.getMinutes() |
getSeconds() | Seconds | date.getSeconds() |
Example:
Setting Date Values
Formatting Dates
Convert date to readable string formats:
Working With Time
Date Calculations
Example: Add 7 days:
Summary
JS Dates allow you to:
Create and store dates/time
Get or set year, month, day, hour, etc.
Format dates in readable forms
Perform time calculations (add days, compare dates, etc.)
