Emmanuel Akinfulubi

JavaScriptDate

Get the day of the year in JavaScript

Intermediate · 4 min read

The day of the year numbers every day from 1 on 1 January to 365 on 31 December, or 366 in a leap year. Habit trackers use it for "day 42 of 365". Weather and science data often label each day by this number. Some games and puzzles change their content by it each day.

In this tutorial you will write dayOfYear(date), and then go the other way with fromDayOfYear(year, day), which turns a number back into a date. Both are short, and both rely on things you have seen before: counting days with Date.UTC, and dates rolling over when a day is out of range. You need to know how to make a date with new Date(year, month, day) and that months count from 0.

Counting days safely with Date.UTC

To count the days between two dates, you subtract their timestamps (milliseconds since 1970) and divide by the milliseconds in a day, 86,400,000. In local time that can go wrong: places with daylight saving have one 23-hour day and one 25-hour day each year, so the division comes out as a fraction.

Date.UTC(year, month, day) avoids that. It gives the timestamp for midnight in UTC, the world's reference time, which never changes its clocks. Every UTC day is exactly 86,400,000 milliseconds, so the division is always a whole number.

JavaScript

const MS_PER_DAY = 86400000;
console.log((Date.UTC(2026, 0, 2) - Date.UTC(2026, 0, 1)) / MS_PER_DAY); // 1

Counting from 1, not from 0

Days elapsed since 1 January is 0 on 1 January itself. The day of the year is 1 on that date. So the day of the year is "days since 1 January, plus one". Remember that plus one: it is the whole trick.

Here is the exercise. Try it yourself first if you like, then read on and we will build it together.

37 Which day of the year is it?

fair

Return 1 for 1 January and 365 or 366 for 31 December. Then go the other way: turn a day-of-year number back into a date.

Stuck? Three hints, smallest first
  1. 1. Count the days from 1 January and add one, so that 1 January is day 1 rather than day 0.
  2. 2. Normalise both ends to Date.UTC so a clock change cannot make the division fractional.
  3. 3. For the inverse, remember that the constructor rolls day 60 of January into 1 March.

Step 1: days since 1 January

Turn the date and 1 January of the same year into UTC midnights, and count the days between them:

Step 1

function daysSinceNewYear(date) {
  const firstOfJanuary = Date.UTC(date.getFullYear(), 0, 1);
  const thisDay = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
  return (thisDay - firstOfJanuary) / MS_PER_DAY;
}
console.log(daysSinceNewYear(new Date(2026, 0, 1)));  // 0
console.log(daysSinceNewYear(new Date(2026, 0, 15))); // 14

The date's local year, month and day go into Date.UTC, so the time of day is thrown away. 15 January is 14 days after 1 January. That is the off-by-one you need to fix.

Step 2: add one

Step 2

function dayOfYear(date) {
  const firstOfJanuary = Date.UTC(date.getFullYear(), 0, 1);
  const thisDay = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
  return (thisDay - firstOfJanuary) / MS_PER_DAY + 1;   // + 1 so 1 January is day 1
}
console.log(dayOfYear(new Date(2026, 0, 1)));   // 1
console.log(dayOfYear(new Date(2026, 0, 15)));  // 15

Now 1 January is day 1 and 15 January is day 15, as the calendar says.

Step 3: leap years shift everything after February

Step 3

console.log(dayOfYear(new Date(2026, 2, 1)));   // 60
console.log(dayOfYear(new Date(2024, 2, 1)));   // 61
console.log(dayOfYear(new Date(2026, 11, 31))); // 365
console.log(dayOfYear(new Date(2024, 11, 31))); // 366

In 2026, January has 31 days and February 28, so 1 March is day 60. In 2024, February has 29 days, so 1 March is day 61, and every later date is one higher. You didn't write any leap year code: the calendar inside Date.UTC took care of it.

Step 4: from a number back to a date

To go the other way, ask for "day N of January". January only has 31 days, but dates roll over, so day 60 of January carries on into the following months:

Step 4

const fromDayOfYear = (year, day) => new Date(year, 0, day);
const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');
console.log(iso(fromDayOfYear(2026, 60)));  // '2026-03-01'
console.log(iso(fromDayOfYear(2026, 200))); // '2026-07-19'

iso prints a date as YYYY-MM-DD. Day 60 of 2026 is 1 March, which matches Step 3. Day 200 is 19 July.

Step 5: round trips and the edge

Each function undoes the other:

Step 5

console.log(dayOfYear(fromDayOfYear(2026, 200))); // 200
console.log(iso(fromDayOfYear(2026, 366)));      // '2027-01-01'

Day 366 of a common year doesn't exist, so it rolls into 1 January of the next year. If you are stepping through days, that is what you want. If you are checking a number someone typed in, it is something to guard against, for example by checking that the result's year is still the year you asked for.

The full solution

The panel has dayOfYear and fromDayOfYear. It prints a few days in 2026 and 2024, then checks the first and last days, 1 March in both kinds of year, the round trip and the day-366 rollover. Press Run to see the results.

The answer

const MS_PER_DAY = 86400000;

function dayOfYear(date) {
  const firstOfJanuary = Date.UTC(date.getFullYear(), 0, 1);
  const thisDay = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
  return (thisDay - firstOfJanuary) / MS_PER_DAY + 1;   // + 1 so 1 January is day 1
}

// The inverse, leaning on the constructor's overflow: day 60 of January is
// 1 March in a common year, which is exactly the date wanted.
const fromDayOfYear = (year, day) => new Date(year, 0, day);

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

console.log('01/01/2026 ->', dayOfYear(new Date(2026, 0, 1)));
console.log('01/03/2026 ->', dayOfYear(new Date(2026, 2, 1)));
console.log('01/03/2024 ->', dayOfYear(new Date(2024, 2, 1)), '<- one more, 29 February is behind it');
console.log('day 60 of 2026 ->', iso(fromDayOfYear(2026, 60)));

Output


      
9 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 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('1 January is day 1, not day 0', dayOfYear(new Date(2026, 0, 1)), 1);
test('15 January is day 15', dayOfYear(new Date(2026, 0, 15)), 15);
test('1 March in a common year is day 60', dayOfYear(new Date(2026, 2, 1)), 60);
test('and day 61 in a leap year', dayOfYear(new Date(2024, 2, 1)), 61);
test('31 December of a common year is 365', dayOfYear(new Date(2026, 11, 31)), 365);
test('and 366 of a leap year', dayOfYear(new Date(2024, 11, 31)), 366);
test('the inverse round-trips', iso(fromDayOfYear(2026, 60)), '2026-03-01');
test('and back again', dayOfYear(fromDayOfYear(2026, 200)), 200);
test('day 366 of a common year spills into the next', iso(fromDayOfYear(2026, 366)), '2027-01-01');

The + 1 is the only thing to get right, and it is the difference between an ordinal day number and an elapsed-days count. Day-of-year is defined so that 1 January is 1.

fromDayOfYear is a good illustration of overflow used deliberately: new Date(2026, 0, 200) means "the 200th day of January", which the constructor normalises to 19 July. The last assertion shows the boundary — day 366 of a common year is 1 January of the next, which is exactly what you want if you are stepping and exactly what you must guard against if you are validating.

Common mistakes

  • Forgetting the + 1. 1 January comes out as day 0.
  • Subtracting local dates. In a zone with daylight saving, one division a year comes out as a fraction. Use Date.UTC.
  • Trusting fromDayOfYear with any number. Day 366 of a common year silently becomes next year.

Try it yourself

  1. Write daysLeftInYear(date) that returns 0 on 31 December.
  2. Print the day of the year for your birthday this year.
  3. Write isValidDayOfYear(year, day) that uses fromDayOfYear and checks the year didn't change.

Keep practising

Newsletter

New writing, when it lands.

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