Skip to main content

Command Palette

Search for a command to run...

Why a Day Isn't 86,400 Seconds in Programming

Updated
5 min readView as Markdown
Why a Day Isn't 86,400 Seconds in Programming

Quick Answer: Assuming a day is exactly 86,400 seconds will inevitably break your software. Time is a political construct, not a mathematical one. Irregularities like daylight savings, sudden government time zone shifts, and complex leap year rules mean developers should never hardcode time calculations and must always use well-maintained time libraries.

If you were to ask me how long a day is, the quick math usually goes like this: 24 hours multiplied by 60 minutes multiplied by 60 seconds. That gives you 86,400 seconds. It makes logical sense, but in software engineering, relying on that number is a massive trap. Hardcoding time math is like trying to measure a stretching rubber band with a straight wooden ruler—the underlying material is constantly shifting.

Why is calculating time in software so difficult?

Calculating time is difficult because time is a political construct rather than a strict mathematical one. Governments regularly alter how time is measured to suit local needs, introducing irregularities that break fixed mathematical formulas.

The most obvious example of this friction is Daylight Savings Time (DST). Let's say you have a background cron job that you want to run at 1:00 AM every day. If you program that job to execute every 86,400 seconds, your scheduling will silently break the moment a DST transition occurs. When the clocks spring forward or fall back, your job will suddenly start running at 2:00 AM or midnight instead. You didn't change your code, but the political definition of the day shifted underneath you, causing a localized bug.

How do political time zone changes impact applications?

Sudden government changes to time zones can cause scheduling failures, data misalignment, and dropped events if your application relies on outdated time databases. Countries frequently shift offsets or skip entire calendar days with very little warning to developers.

Time zone shenanigans happen constantly. Because time belongs to politics, governments can just change the rules on a whim. Imagine your team is building a global scheduling application for a logistics company. If you assume time zones are static geographic lines, you are going to run into severe data corruption.

Here are a few notable instances where political time decisions broke software expectations:

  • Samoa (2011): Samoa decided it wanted to shift across the International Date Line to align closer to Australia's trading hours. Because they sat at the extreme edge of the time zone spectrum, they simply skipped December 30th entirely. They just lost a whole day.
  • Venezuela (2007 & 2016): The government shifted its time zone by exactly 30 minutes, giving the global tech community only a few weeks to figure out how to patch their systems.
  • Egypt (2016 & 2023): Egypt decided to completely abolish daylight savings time in 2016, only to bring it back in 2023. If you weren't aggressively updating your local time zone databases, every single timestamp processed for users in Egypt during those transitions was wrong.

How do leap years and leap seconds break time math?

Leap years and leap seconds introduce irregular intervals that ruin naive mathematical calculations. The rules for calculating leap years have multiple exceptions, and leap seconds are added unpredictably to keep clocks aligned with the Earth's rotation.

Even standard calendar rules are more convoluted than we are taught in school. We all know a leap year happens every four years. Except, that is not actually true. A leap year happens every four years, unless the year is divisible by 100. But wait, there is another exception: it is a leap year if that year is also divisible by 400.

Then you have leap seconds. The Earth's rotation is not perfectly consistent. To account for this slight wobble, the scientific community occasionally just adds an extra second to the clock. When a minute suddenly has 61 seconds, any system heavily relying on 86,400-second intervals will drift or crash.

What is the best way to handle time and dates in code?

The absolute best approach is to rely entirely on standard, well-maintained date and time libraries rather than writing custom mathematical calculations. You must also ensure the underlying time zone database your system relies on is continuously updated.

You should never calculate time offsets yourself. If you need to add a day to a timestamp, let the language's native date abstractions or a dedicated library handle the calendar logic.

// Trap: Hardcoding seconds ignores DST and leap anomalies
const tomorrow = new Date(Date.now() + (86400 * 1000));

// Fix: Using native date methods respects calendar logic
const date = new Date();
date.setDate(date.getDate() + 1);

A good time library knows about Samoa's missing day. It knows about Egypt's changing rules. It knows that the day might only be 23 hours long tomorrow. Rely on the maintainers who track global time politics so you don't have to.

FAQ

Why shouldn't I use 86,400 seconds to calculate a day?

Because days are not always 24 hours long. Daylight savings time transitions mean some days are 23 hours and some are 25 hours. Using a fixed number of seconds will cause your timestamps and scheduled background jobs to drift off target.

What is the tz database?

The Time Zone Database (often called the tz database or IANA database) is a collaborative, global compilation of information about the world's time zones. It contains the historical and current rules for daylight savings and offsets, which governments update frequently.

How do you programmatically calculate a leap year?

A year is a leap year if it is evenly divisible by 4. However, if it is divisible by 100, it is not a leap year, unless it is also divisible by 400. Most modern date libraries handle this complex math automatically under the hood so you don't have to write it yourself.