Date Utilities in JavaScript

Common date utility functions

BeginnerDate/Time ProgramsExample 10 of 10
date-utilities.js
1// Method 1: Get start/end of day
2function startOfDay(date) {
3 const d = new Date(date);
4 d.setHours(0, 0, 0, 0);
5 return d;
6}
7
8function endOfDay(date) {
9 const d = new Date(date);
10 d.setHours(23, 59, 59, 999);
11 return d;
12}
13
14const today = new Date();
15console.log('Start of day:', startOfDay(today));
16console.log('End of day:', endOfDay(today));
17
18// Method 2: Get start/end of week
19function 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}
25
26function endOfWeek(date) {
27 const start = startOfWeek(date);
28 return new Date(start.getTime() + 6 * 24 * 60 * 60 * 1000);
29}
30
31console.log('Start of week:', startOfWeek(today));
32console.log('End of week:', endOfWeek(today));
33
34// Method 3: Get start/end of month
35function startOfMonth(date) {
36 return new Date(date.getFullYear(), date.getMonth(), 1);
37}
38
39function endOfMonth(date) {
40 return new Date(date.getFullYear(), date.getMonth() + 1, 0);
41}
42
43console.log('Start of month:', startOfMonth(today));
44console.log('End of month:', endOfMonth(today));
45
46// Method 4: Get start/end of year
47function startOfYear(date) {
48 return new Date(date.getFullYear(), 0, 1);
49}
50
51function endOfYear(date) {
52 return new Date(date.getFullYear(), 11, 31);
53}
54
55console.log('Start of year:', startOfYear(today));
56console.log('End of year:', endOfYear(today));
57
58// Method 5: Days in month
59function daysInMonth(date) {
60 return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
61}
62
63console.log('Days in month:', daysInMonth(today));
64
65// Method 6: Is leap year
66function isLeapYear(year) {
67 return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
68}
69
70console.log('Is leap year:', isLeapYear(2024));
71
72// Method 7: Get day name
73function getDayName(date, locale = 'en-US') {
74 return date.toLocaleDateString(locale, { weekday: 'long' });
75}
76
77console.log('Day name:', getDayName(today));
78
79// Method 8: Get month name
80function getMonthName(date, locale = 'en-US') {
81 return date.toLocaleDateString(locale, { month: 'long' });
82}
83
84console.log('Month name:', getMonthName(today));
85
86// Method 9: Is weekend
87function isWeekend(date) {
88 const day = date.getDay();
89 return day === 0 || day === 6;
90}
91
92console.log('Is weekend:', isWeekend(today));
93
94// Method 10: Is weekday
95function isWeekday(date) {
96 return !isWeekend(date);
97}
98
99console.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