Date/Time Programs
Date is a mess. Month is 0-indexed. That bug ships.
10 programs · open one, then the next
Start with Get Current Date and Time- 01Get Current Date and TimeBeginnerGet current date and time in various formatsOpen example →js// Method 1: Current date and time const now = new Date(); console.log('Current date:', now);
- 02Format Date and TimeBeginnerFormat dates in various formatsOpen example →js// Method 1: Basic formatting function formatDate(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0');
- 03Date ArithmeticBeginnerAdd and subtract time from datesOpen example →js// Method 1: Add/subtract days function addDays(date, days) { const result = new Date(date); result.setDate(result.getDate() + days);
- 04Compare DatesBeginnerCompare two datesOpen example →js// Method 1: Compare timestamps function compareDates(date1, date2) { if (date1.getTime() === date2.getTime()) { return 0; // Equal
- 05Timezone ConversionIntermediateConvert dates between timezonesOpen example →js// Method 1: Get timezone offset function getTimezoneOffset() { const offset = new Date().getTimezoneOffset(); const hours = Math.floor(Math.abs(offset) / 60);
- 06Countdown TimerIntermediateCreate a countdown timerOpen example →js// Method 1: Simple countdown function countdown(targetDate, callback) { const interval = setInterval(() => { const now = new Date().getTime();
- 07StopwatchIntermediateCreate a stopwatchOpen example →js// Method 1: Simple stopwatch class Stopwatch { constructor() { this.startTime = null;
- 08Calculate AgeBeginnerCalculate age from birthdateOpen example →js// Method 1: Simple age calculation function calculateAge(birthDate) { const today = new Date(); let age = today.getFullYear() - birthDate.getFullYear();
- 09Date ValidationBeginnerValidate date inputsOpen example →js// Method 1: Check if valid date function isValidDate(date) { return date instanceof Date && !isNaN(date.getTime()); }
- 10Date UtilitiesBeginnerCommon date utility functionsOpen example →js// Method 1: Get start/end of day function startOfDay(date) { const d = new Date(date); d.setHours(0, 0, 0, 0);