Working with Dates and Times
The Date object lets you work with dates and times in JavaScript.
You can create, format, and manipulate dates easily.
// Creating Dates
const now = new Date(); // Current date/time
const specific = new Date('2024-12-25'); // Specific date
const fromValues = new Date(2024, 11, 25); // Year, month (0-11), day
// Getting Components
now.getFullYear(); // 2024
now.getMonth(); // 0-11 (0 = January)
now.getDate(); // 1-31 (day of month)
now.getDay(); // 0-6 (0 = Sunday)
now.getHours(); // 0-23
now.getMinutes(); // 0-59
now.getSeconds(); // 0-59
// Setting Components
now.setFullYear(2025);
now.setMonth(5); // June (0-based)
now.setDate(15);
// Formatting
now.toString(); // Full string
now.toDateString(); // Date only
now.toTimeString(); // Time only
now.toLocaleDateString(); // Locale date
// Timestamps
Date.now(); // Current timestamp
now.getTime(); // Date as timestamp
// Date arithmetic
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
- new Date() - Create date object
- getFullYear/Month/Date - Get components
- setFullYear/Month/Date - Set components
- toDateString() - Format as string
- Date.now() - Current timestamp
- getTime() - Convert to timestamp
- Month is 0-indexed (0=Jan, 11=Dec)
Try Date Object
<div class="date-demo">
<h3>Date & Time Tools</h3>
<button onclick="showCurrentTime()">🕐 Current Time</button>
<button onclick="calculateAge()">🎂 Calculate Age</button>
<button onclick="countdown()">⏰ Days Until New Year</button>
<div id="dateOutput"></div>
</div>
Note: Remember: Months are 0-indexed! January = 0, December = 11.