How to Get the Start and End of the Day in JavaScript
A common requirement in date-based logic is to find the exact start (00:00:00.000) or end (23:59:59.999) of a given day. This is essential for creating date-range queries, setting expirations, or normalizing timestamps.
When performing this operation, you must make a critical decision: should the start/end be calculated in UTC or in the user's local timezone? This guide will explain the difference and teach you how to create a single, robust function to handle both scenarios.
The Core Concept: UTC vs. Local Time
This is the most important concept to understand:
- UTC (Coordinated Universal Time): This is the universal time standard. A UTC timestamp represents the same moment in time for everyone on the planet. For server-side logic, database storage, and API communication, UTC is the recommended standard because it is unambiguous.
- Local Time: This is the time in the user's specific timezone (e.g., the timezone set on their computer). It is useful for displaying dates and times in a UI but should be avoided for storing or transmitting data, as "midnight" in New York is a different moment in time than "midnight" in Tokyo.
JavaScript's Date object has two parallel sets of methods to handle this:
setHours(),getHours(), etc. (for local time)setUTCHours(),getUTCHours(), etc. (for UTC)
The Modern Solution: A Reusable Function
The best practice is to create a single, reusable function that can calculate the start or end of the day and is parameterized to work in either UTC or local time.
The Reusable Function:
/**
* Gets the start or end of the day for a given date.
* @param {Date} date The date.
* @param {object} [options] Optional configuration.
* @param {boolean} [options.end=false] If true, returns the end of the day.
* @param {boolean} [options.utc=true] If true, performs the calculation in UTC.
* @returns {Date} The new Date object.
*/
function getDayBoundary(date, options = {}) {
const { end = false, utc = true } = options;
// Clone the date to avoid mutating the original
const newDate = new Date(date.getTime());
const setHours = utc ? 'setUTCHours' : 'setHours';
if (end) {
newDate[setHours](23, 59, 59, 999);
} else {
newDate[setHours](0, 0, 0, 0);
}
return newDate;
}
Practical Examples
With our getDayBoundary function, getting the start or end of any day becomes simple and readable.
How to Get the Start/End of the Day in UTC (Recommended)
This is the standard approach for backend and API logic.
For example, we want to find the UTC timestamps for the beginning and end of today.
The solution:
const today = new Date();
// Get the start of the day in UTC (default behavior)
const startOfDayUTC = getDayBoundary(today);
// Get the end of the day in UTC
const endOfDayUTC = getDayBoundary(today, { end: true });
console.log('Start of Day (UTC):', startOfDayUTC.toISOString());
console.log('End of Day (UTC):', endOfDayUTC.toISOString());
Output:
Start of Day (UTC): 2025-10-16T00:00:00.000Z
End of Day (UTC): 2025-10-16T23:59:59.999Z
How to Get the Start/End of the Day in Local Time
This is useful when your logic is purely client-side and needs to align with the user's calendar day.
For example, we want to find the timestamps for the beginning and end of today, according to the user's local timezone.
As solution, we pass the utc: false option to our function.
const today = new Date();
// Get the start of the day in the user's local time
const startOfDayLocal = getDayBoundary(today, { utc: false });
// Get the end of the day in the user's local time
const endOfDayLocal = getDayBoundary(today, { end: true, utc: false });
console.log('Start of Day (Local):', startOfDayLocal.toString());
console.log('End of Day (Local):', endOfDayLocal.toString());
Output:
Start of Day (Local): Thu Oct 16 2025 00:00:00 GMT...
End of Day (Local): Thu Oct 16 2025 23:59:59 GMT...
Conclusion: Which Method Should You Use?
The choice depends entirely on your use case, but the best practice is clear.
- For all backend logic, API communication, and database storage, you should always use UTC. It is the only way to ensure your timestamps are consistent and unambiguous across different servers and users. The
setUTCHours()method is the correct tool for this. - For UI display logic that needs to align with a user's local calendar, it can be appropriate to use local time. The
setHours()method is the tool for this.
By encapsulating the logic in a single, reusable function, you can write clean, readable, and reliable code that handles both scenarios correctly.