Date Utilities in JavaScript
Common date utility functions
BeginnerDate/Time ProgramsExample 10 of 10
date-utilities.js
1// Method 1: Get start/end of day2function startOfDay(date) {3 const d = new Date(date);4 d.setHours(0, 0, 0, 0);5 return d;6}78function endOfDay(date) {9 const d = new Date(date);10 d.setHours(23, 59, 59, 999);11 return d;12}1314const today = new Date();15console.log('Start of day:', startOfDay(today));16console.log('End of day:', endOfDay(today));1718// Method 2: Get start/end of week19function startOfWeek(date) {20 const d = new Date(date);21 const day = d.getDay();22 const diff = d.getDate() - day;23 return new Date(d.setDate(diff));24}2526function endOfWeek(date) {27 const start = startOfWeek(date);28 return new Date(start.getTime() + 6 * 24 * 60 * 60 * 1000);29}3031console.log('Start of week:', startOfWeek(today));32console.log('End of week:', endOfWeek(today));3334// Method 3: Get start/end of month35function startOfMonth(date) {36 return new Date(date.getFullYear(), date.getMonth(), 1);37}3839function endOfMonth(date) {40 return new Date(date.getFullYear(), date.getMonth() + 1, 0);41}4243console.log('Start of month:', startOfMonth(today));44console.log('End of month:', endOfMonth(today));4546// Method 4: Get start/end of year47function startOfYear(date) {48 return new Date(date.getFullYear(), 0, 1);49}5051function endOfYear(date) {52 return new Date(date.getFullYear(), 11, 31);53}5455console.log('Start of year:', startOfYear(today));56console.log('End of year:', endOfYear(today));5758// Method 5: Days in month59function daysInMonth(date) {60 return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();61}6263console.log('Days in month:', daysInMonth(today));6465// Method 6: Is leap year66function isLeapYear(year) {67 return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);68}6970console.log('Is leap year:', isLeapYear(2024));7172// Method 7: Get day name73function getDayName(date, locale = 'en-US') {74 return date.toLocaleDateString(locale, { weekday: 'long' });75}7677console.log('Day name:', getDayName(today));7879// Method 8: Get month name80function getMonthName(date, locale = 'en-US') {81 return date.toLocaleDateString(locale, { month: 'long' });82}8384console.log('Month name:', getMonthName(today));8586// Method 9: Is weekend87function isWeekend(date) {88 const day = date.getDay();89 return day === 0 || day === 6;90}9192console.log('Is weekend:', isWeekend(today));9394// Method 10: Is weekday95function isWeekday(date) {96 return !isWeekend(date);97}9899console.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
What's going on
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