Date Utilities

Common date utility functions

JavaScriptBeginner
JavaScript
// Method 1: Get start/end of day
function startOfDay(date) {
    const d = new Date(date);
    d.setHours(0, 0, 0, 0);
    return d;
}

function endOfDay(date) {
    const d = new Date(date);
    d.setHours(23, 59, 59, 999);
    return d;
}

const today = new Date();
console.log('Start of day:', startOfDay(today));
console.log('End of day:', endOfDay(today));

// Method 2: Get start/end of week
function startOfWeek(date) {
    const d = new Date(date);
    const day = d.getDay();
    const diff = d.getDate() - day;
    return new Date(d.setDate(diff));
}

function endOfWeek(date) {
    const start = startOfWeek(date);
    return new Date(start.getTime() + 6 * 24 * 60 * 60 * 1000);
}

console.log('Start of week:', startOfWeek(today));
console.log('End of week:', endOfWeek(today));

// Method 3: Get start/end of month
function startOfMonth(date) {
    return new Date(date.getFullYear(), date.getMonth(), 1);
}

function endOfMonth(date) {
    return new Date(date.getFullYear(), date.getMonth() + 1, 0);
}

console.log('Start of month:', startOfMonth(today));
console.log('End of month:', endOfMonth(today));

// Method 4: Get start/end of year
function startOfYear(date) {
    return new Date(date.getFullYear(), 0, 1);
}

function endOfYear(date) {
    return new Date(date.getFullYear(), 11, 31);
}

console.log('Start of year:', startOfYear(today));
console.log('End of year:', endOfYear(today));

// Method 5: Days in month
function daysInMonth(date) {
    return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
}

console.log('Days in month:', daysInMonth(today));

// Method 6: Is leap year
function isLeapYear(year) {
    return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}

console.log('Is leap year:', isLeapYear(2024));

// Method 7: Get day name
function getDayName(date, locale = 'en-US') {
    return date.toLocaleDateString(locale, { weekday: 'long' });
}

console.log('Day name:', getDayName(today));

// Method 8: Get month name
function getMonthName(date, locale = 'en-US') {
    return date.toLocaleDateString(locale, { month: 'long' });
}

console.log('Month name:', getMonthName(today));

// Method 9: Is weekend
function isWeekend(date) {
    const day = date.getDay();
    return day === 0 || day === 6;
}

console.log('Is weekend:', isWeekend(today));

// Method 10: Is weekday
function isWeekday(date) {
    return !isWeekend(date);
}

console.log('Is weekday:', isWeekday(today));

Output

Start of day: 2024-01-15T00:00:00.000Z
End of day: 2024-01-15T23:59:59.999Z
Start of week: 2024-01-14T00:00:00.000Z
End of week: 2024-01-20T23:59:59.999Z
Start of month: 2024-01-01T00:00:00.000Z
End of month: 2024-01-31T23:59:59.999Z
Start of year: 2024-01-01T00:00:00.000Z
End of year: 2024-12-31T23:59:59.999Z
Days in month: 31
Is leap year: true
Day name: Monday
Month name: January
Is weekend: false
Is weekday: true

Date utilities provide common operations.

Range Functions

  • Start/end of day
  • Start/end of week
  • Start/end of month
  • Start/end of year

Date Information

  • Days in month
  • Is leap year
  • Day name
  • Month name

Day Checks

  • Is weekend
  • Is weekday
  • Day of week

Use Cases

  • Calendar apps
  • Date ranges
  • Reporting
  • Scheduling

Best Practices

  • Create new Date objects
  • Don't mutate originals
  • Handle timezones
  • Use locale for names