Date Utilities in JavaScript
Write JavaScript date utilities: start and end of day, week, month and year, days in a month, leap years and weekend checks. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
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(); // 0 = Sunday22 d.setDate(d.getDate() - day);23 d.setHours(0, 0, 0, 0);24 return d;25}2627function endOfWeek(date) {28 const end = startOfWeek(date);29 end.setDate(end.getDate() + 6);30 end.setHours(23, 59, 59, 999);31 return end;32}3334console.log('Start of week:', startOfWeek(today));35console.log('End of week:', endOfWeek(today));3637// Method 3: Get start/end of month38function startOfMonth(date) {39 return new Date(date.getFullYear(), date.getMonth(), 1);40}4142function endOfMonth(date) {43 return new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999);44}4546console.log('Start of month:', startOfMonth(today));47console.log('End of month:', endOfMonth(today));4849// Method 4: Get start/end of year50function startOfYear(date) {51 return new Date(date.getFullYear(), 0, 1);52}5354function endOfYear(date) {55 return new Date(date.getFullYear(), 11, 31, 23, 59, 59, 999);56}5758console.log('Start of year:', startOfYear(today));59console.log('End of year:', endOfYear(today));6061// Method 5: Days in month62function daysInMonth(date) {63 return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();64}6566console.log('Days in month:', daysInMonth(today));6768// Method 6: Is leap year69function isLeapYear(year) {70 return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);71}7273console.log('Is leap year:', isLeapYear(2024));7475// Method 7: Get day name76function getDayName(date, locale = 'en-US') {77 return date.toLocaleDateString(locale, { weekday: 'long' });78}7980console.log('Day name:', getDayName(today));8182// Method 8: Get month name83function getMonthName(date, locale = 'en-US') {84 return date.toLocaleDateString(locale, { month: 'long' });85}8687console.log('Month name:', getMonthName(today));8889// Method 9: Is weekend90function isWeekend(date) {91 const day = date.getDay();92 return day === 0 || day === 6;93}9495console.log('Is weekend:', isWeekend(today));9697// Method 10: Is weekday98function isWeekday(date) {99 return !isWeekend(date);100}101102console.log('Is weekday:', isWeekday(today));
Sample 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
In this one
- How to find the start and end of a day, week, month and year
- How the day-0 trick finds the last day of a month
- How to get localised day and month names
- How to check weekends and leap years
How it works
- 1
startOfDay and endOfDay give 00:00 and 23:59:59.999.
- 2
startOfWeek moves back to Sunday at midnight.
- 3
endOfWeek gives Saturday at the last millisecond.
- 4
Month and year helpers give the first and last moments.
- 5
daysInMonth gives 31 and isLeapYear(2024) gives true.
- 6
Day name, month name and weekend checks are printed.
Edge cases
Weeks starting Monday
startOfWeek returns Sunday.
Use (day + 6) % 7 as the offset to start on Monday.
End of range at midnight
new Date(y, m + 1, 0) misses everything after 00:00 on the last day.
Set 23:59:59.999, or compare with < start of the next month.
Time zones
Output in UTC can look like a different day in India.
These helpers use local time. Print with toLocaleString to see local values.
What's going on
Calendar apps, attendance reports and timetables keep asking the same questions. When does this week start? How many days are in this month? Is it a weekend? Date utilities are small helper functions that answer them. Each one copies or creates a new Date, so the original is never mutated.
Method 1 gives the start and end of a day. setHours(0, 0, 0, 0) moves to midnight. setHours(23, 59, 59, 999) moves to the last millisecond. These are useful for range searches like "all submissions today".
Method 2 gives the week. getDay() returns 0 for Sunday, so subtracting it from the date gives the Sunday of this week. The week helper also sets the time to midnight. endOfWeek adds six days and moves to the end of that day. Some countries start weeks on Monday, so adjust if needed.
Method 3 gives the month. Day 1 is the start. For the end, it asks for day 0 of next month, which JavaScript turns into the last day of this month. The end-of-month helper sets the time to 23:59:59.999 so the whole last day is included. Method 4 does the same for the year.
Method 5 uses the day-0 trick again to count days in the month: 31 for January.
Method 6 checks leap years with the 4, 100 and 400 rule.
Methods 7 and 8 get names with toLocaleDateString and the weekday or month option. Pass a locale like hi-IN to get names in Hindi.
Methods 9 and 10 check weekends and weekdays. A weekend is day 0 or 6.
The end-of-month and end-of-year helpers set the time to the last moment of the day, so the last day is included. The week helpers reset the clock time.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which day number is Saturday?
Pick an answer to check it.
Question 2
How many days does daysInMonth give for February 2024?
Pick an answer to check it.
Questions people ask
What does new Date(2024, 1, 0) give?
31 January 2024, the day before 1 February.
Can I get names in another language?
Yes. getDayName(date, 'hi-IN') returns the Hindi weekday.
Why return new Date objects?
So callers can change the result without affecting the input.