Emmanuel Akinfulubi

JavaScriptDate

Get the start and end of a day in JavaScript

Intermediate · 4 min read

"Show me today's orders" really means "every order from midnight this morning up to the last moment before midnight tonight". To ask that question, you need two dates: the start of the day and the end of the day. They are probably the two most-used helpers in any date code.

In this tutorial you will write startOfDay(date) and endOfDay(date). Both return a new date and leave the input alone. You will also see a small slip, forgetting the milliseconds, that makes range checks fail in a way that is very hard to spot. You need to know how to make a date with new Date(year, month, day, hour, minute, second, ms) and that you should copy a date before changing it.

setHours can set four things at once

setHours changes the hour of a date. It also accepts the minutes, seconds and milliseconds as extra arguments, in that order:

JavaScript

const d = new Date(2026, 0, 15, 15, 42, 17, 88);
d.setHours(9, 30, 0, 0);
console.log(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()); // 9 30 0 0

One call, four parts. The date stays on the same day. Like every set method, it changes the date it is called on, so you copy first.

The last moment of a day

A JavaScript date counts down to the millisecond, a thousandth of a second. The last moment of a day is therefore 23:59:59.999, not 23:59:59, and not 24:00. Midnight at 24:00 is really the start of the next day.

Here is the exercise. Have a go on your own first if you like, then read on and we will build it together.

33 The start and the end of a day

fair

Return midnight at the start of a date's day, and the last millisecond of it, without changing the input.

Stuck? Three hints, smallest first
  1. 1. setHours takes minutes, seconds and milliseconds as further arguments, so one call does all four.
  2. 2. The last millisecond is 23:59:59.999, not 24:00:00.
  3. 3. Copy first. These are the two most-copied helpers in any date module.

Step 1: the start of the day

Copy the date, then set the hours, minutes, seconds and milliseconds all to zero:

Step 1

function startOfDay(date) {
  const copy = new Date(date);
  copy.setHours(0, 0, 0, 0);          // hours, minutes, seconds, milliseconds
  return copy;
}
const afternoon = new Date(2026, 0, 15, 15, 42, 17, 88);
const start = startOfDay(afternoon);
console.log(start.getHours(), start.getMinutes(), start.getSeconds(), start.getMilliseconds()); // 0 0 0 0
console.log(start.getDate()); // 15

The time is now midnight at the very start of 15 January. The calendar date didn't move.

Step 2: the wrong turn, forgetting the milliseconds

It is easy to write only three zeros:

Step 2

const almost = new Date(afternoon);
almost.setHours(0, 0, 0);
console.log(almost.getMilliseconds()); // 88

When you leave out an argument, setHours keeps that part as it was. The input had 88 milliseconds, so the result is 00:00:00.088. On screen it looks exactly like midnight. But a check like "is this order at or after the start of the day?" now misses anything in those first 88 milliseconds. A bug like that may appear once in a long while and be almost impossible to reproduce.

Step 3: the end of the day

The same idea, with the largest values instead of zeros:

Step 3

function endOfDay(date) {
  const copy = new Date(date);
  copy.setHours(23, 59, 59, 999);
  return copy;
}
const end = endOfDay(afternoon);
console.log(end.getHours(), end.getMinutes(), end.getSeconds(), end.getMilliseconds()); // 23 59 59 999
console.log(end.getDate()); // 15

Why not setHours(24, 0, 0, 0)? Try it:

JavaScript

const tooFar = new Date(afternoon);
tooFar.setHours(24, 0, 0, 0);
console.log(tooFar.getDate()); // 16

Hour 24 rolls over into the next day, so you would be on the 16th.

Step 4: check them against the input

The start should be at or before the original moment, and the end at or after it. The input itself should be unchanged:

Step 4

console.log(start <= afternoon);    // true
console.log(end >= afternoon);      // true
console.log(afternoon.getHours());  // 15

<= and >= compare dates by their timestamps. The input is still at 15:42, because both helpers work on a copy.

Step 5: how long is a day?

Subtract the start from the end:

Step 5

console.log(end - start); // 86399999

That is one millisecond less than 86,400,000, the number of milliseconds in 24 hours. Be careful with that number, though. In places that move their clocks for daylight saving, one day a year is an hour shorter and another is an hour longer. 15 January is a safe example because no clock change happens on that date. Don't assume every local day is 24 hours long.

The full solution

The panel has both helpers and prints the start and end of 15 January next to the original afternoon time. The checks cover the time parts, the unchanged date, the order of the three moments, the length of the day and the untouched input. Press Run to see them.

The answer

function startOfDay(date) {
  const copy = new Date(date);
  copy.setHours(0, 0, 0, 0);          // hours, minutes, seconds, milliseconds
  return copy;
}

function endOfDay(date) {
  const copy = new Date(date);
  copy.setHours(23, 59, 59, 999);
  return copy;
}

// 15 January 2026, mid-afternoon. A date with no clock change anywhere, which
// is what makes the length assertion below safe to write.
const afternoon = new Date(2026, 0, 15, 15, 42, 17, 88);

console.log('given :', afternoon.toString().slice(0, 24));
console.log('start :', startOfDay(afternoon).toString().slice(0, 24));
console.log('end   :', endOfDay(afternoon).toString().slice(0, 24));
console.log('the input is still', afternoon.getHours() + ':' + afternoon.getMinutes());

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('the start of the day is all zeroes', [startOfDay(afternoon).getHours(), startOfDay(afternoon).getMinutes(), startOfDay(afternoon).getSeconds(), startOfDay(afternoon).getMilliseconds()], [0, 0, 0, 0]);
test('the end of the day is 23:59:59.999', [endOfDay(afternoon).getHours(), endOfDay(afternoon).getMinutes(), endOfDay(afternoon).getSeconds(), endOfDay(afternoon).getMilliseconds()], [23, 59, 59, 999]);
test('the calendar date does not move', startOfDay(afternoon).getDate(), 15);
test('for either of them', endOfDay(afternoon).getDate(), 15);
test('the start is at or before the original', startOfDay(afternoon) <= afternoon, true);
test('and the end is at or after it', endOfDay(afternoon) >= afternoon, true);
test('on a day with no clock change, they are 86399999ms apart', endOfDay(afternoon) - startOfDay(afternoon), 86399999);
test('and the input was not modified', afternoon.getHours(), 15);

setHours(0, 0, 0, 0) in one call is the idiom — forgetting the milliseconds argument leaves a value between 0 and 999 in there, which then makes an otherwise-correct range check fail for one day in a thousand.

The length assertion is deliberately qualified. A local day is not always 86,400,000 milliseconds: in a zone that shifts its clocks, one day a year is an hour shorter and another an hour longer. 15 January is a day on which no zone changes its clocks, which is why the figure can be asserted at all. Never assume a day is 24 hours in local time; that assumption is what exercise 53 pulls apart.

Common mistakes

  • Calling setHours(0, 0, 0) without the milliseconds. The old milliseconds stay behind, and it still looks like midnight.
  • Ending the day at 24:00. Hour 24 is the next day. Use 23:59:59.999.
  • Changing the input. Without a copy, startOfDay(order.time) would rewrite the order's time.

Try it yourself

  1. Write isToday(date, now) that checks date is between startOfDay(now) and endOfDay(now).
  2. Write startOfTomorrow(date) using startOfDay and setDate.
  3. Filter an array of order times to only those on 15 January 2026.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.