Get the number of days in a year in JavaScript
Beginner · 4 min read
A year has 365 days, or 366 in a leap year. Age calculators, interest calculations and "days left this year" counters all need that number. In this tutorial you will work it out in two different ways: once with the leap-year rule, and once by measuring the real gap between two New Year's Days. Then you will check that both ways always agree.
The measuring version has one trap, and it is the most useful thing in this lesson. Measured in local time, a day is not always 24 hours long. You will see where that comes from and how to avoid it. You need to know the leap-year rule and how to make a date with new Date(year, month, day).
Dates are numbers underneath
Every JavaScript date is stored as one number: the milliseconds since midnight UTC at the start of 1 January 1970. You can read it with getTime(). Subtracting two dates gives the milliseconds between them:
const MS_PER_DAY = 24 * 60 * 60 * 1000;
console.log(MS_PER_DAY); // 86400000
24 hours, 60 minutes, 60 seconds and 1,000 milliseconds. Dividing a gap in milliseconds by that number turns it into days.
Local time and UTC
Local time is the clock in your own time zone. Many zones use daylight saving: they move the clock forward an hour in spring and back an hour in autumn. On those two days, the local day is 23 or 25 hours long.
UTC is the world's reference clock. It never changes for daylight saving, so in UTC every day is exactly 86,400,000 milliseconds. Date.UTC(year, month, day) gives you the millisecond number for a UTC date. It takes the same parts as new Date, with months counted from 0.
Here is the exercise. Try it on your own first if you like, then read on and we will build it together.
11 How many days are in a year?
easyReturn 365 or 366 for a given year, then measure the same thing from two timestamps and check the two agree.
Stuck? Three hints, smallest first
- 1. The leap rule gives you the answer directly.
- 2. To measure it instead, subtract 1 January of one year from 1 January of the next.
- 3. Do that subtraction in UTC. A local year containing a clock change is not a whole number of 24-hour days.
Step 1: the rule
Use the leap-year test, then pick 366 or 365:
const isLeapYear = (year) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
const daysInYear = (year) => (isLeapYear(year) ? 366 : 365);
console.log(daysInYear(2026), daysInYear(2024)); // 365 366
console.log(daysInYear(1900), daysInYear(2000)); // 365 366
1900 divides by 4 but is a century year, so it is not a leap year. 2000 divides by 400, so it is.
Step 2: the wrong turn, measuring in local time
To measure a span, subtract its start from its end and divide by the length of a day. Try it on a month first, March 2026, with local dates:
const localDays = (start, end) => (end - start) / MS_PER_DAY;
console.log(localDays(new Date(2026, 2, 1), new Date(2026, 3, 1)));
// 31 in Lagos; 30.958333333333332 in London
March has 31 days, but in London the answer is not a whole number. On 29 March 2026 the clocks in London jump forward one hour, so that local day is only 23 hours long. Lagos doesn't use daylight saving, so there you get 31. The same code gives different answers on different computers.
The answer for a whole year can come out right in local time, because the spring and autumn changes cancel out. But a span that crosses one change and not the other gives a broken result, and you won't notice until someone runs your code in another country.
Step 3: measure in UTC instead
Date.UTC returns plain millisecond numbers in UTC, where every day is exactly the same length:
const measured = (year) => (Date.UTC(year + 1, 0, 1) - Date.UTC(year, 0, 1)) / MS_PER_DAY;
console.log(measured(2026), measured(2024)); // 365 366
The span runs from 1 January of year to 1 January of the next year. You get a whole number in every time zone.
Step 4: check the two methods against each other
You now have two independent ways to answer the same question. Compare them for every year from 1900 to 2100:
const years = Array.from({ length: 201 }, (unused, i) => 1900 + i);
console.log(years.every((y) => daysInYear(y) === measured(y))); // true
Array.from({ length: 201 }, ...) builds the 201 years, and every is true only if all of them match. The rule and the calendar agree for two centuries.
One more check: 2024 to 2027 contain one leap year, so the four years add up to 365 times 4, plus 1:
console.log([2024, 2025, 2026, 2027].reduce((sum, y) => sum + daysInYear(y), 0)); // 1461
The full solution
The panel has both versions, prints three years and runs the 201-year comparison. Press Run to see the output.
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const isLeapYear = (year) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
const daysInYear = (year) => (isLeapYear(year) ? 366 : 365);
// Measured instead of looked up. Date.UTC because in UTC every day is exactly
// 24 hours; a local year with a daylight-saving change is not, so the same
// division on local dates can come out as 364.958.
const measured = (year) => (Date.UTC(year + 1, 0, 1) - Date.UTC(year, 0, 1)) / MS_PER_DAY;
console.log('2026:', daysInYear(2026), '| measured:', measured(2026));
console.log('2024:', daysInYear(2024), '| measured:', measured(2024));
console.log('1900:', daysInYear(1900), '| 2000:', daysInYear(2000));
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.
test('2026 has 365 days', daysInYear(2026), 365);
test('2024 has 366', daysInYear(2024), 366);
test('1900 had 365, despite being divisible by 4', daysInYear(1900), 365);
test('2000 had 366, because 400 divides it', daysInYear(2000), 366);
test('measuring in UTC gives a whole number', measured(2026), 365);
test('and agrees on the leap year', measured(2024), 366);
test('the two never disagree, 1900 to 2100', Array.from({ length: 201 }, (unused, i) => 1900 + i).every((y) => daysInYear(y) === measured(y)), true);
test('four consecutive years hold 1461 days', [2024, 2025, 2026, 2027].reduce((sum, y) => sum + daysInYear(y), 0), 1461);
Both halves are worth having: the rule, because it needs no Date at all, and the measurement, because it checks the rule against the engine's calendar.
The reason the measurement uses Date.UTC is the point to carry forward. UTC has no daylight saving, so every UTC day is exactly 86,400,000 milliseconds and division is exact. Local time does not have that property: in a zone that shifts its clocks, two of the days in the year are 23 and 25 hours long. Every "how many days between" calculation in this section normalises to UTC first for that reason.
Common mistakes
- Measuring days in local time. A span that crosses a daylight saving change is an hour short or long, so the division doesn't give a whole number.
- Rounding the broken result away.
Math.roundhides the symptom here, but the code still depends on the zone. Measure in UTC. - Using only
year % 4 === 0. It gives 366 for 1900 and 2100, which had and will have 365.
Try it yourself
- Write
daysLeftInYear(year, month, day)usingDate.UTCfor both dates. - Measure February 2024 with
Date.UTCand check you get 29. - Add up
daysInYearfor 1901 to 2000. Then work out why the answer is 36,525.