Date Utilities
Common date utility functions
JavaScript Date Utilities Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// 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));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
Understanding Date Utilities
Date utilities provide common operations.
Range Functions
Date Information
Day Checks
Use Cases
Best Practices
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Date Utilities
This JavaScript program is part of the "Date/Time Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.