dash–calculator

04 Numbers and Counting

Days Since 0 in R: Date Arithmetic Explained

Readout 04-0329 · Updated · Standard formula, shown below

The calculator on this page is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

This calculator works out the number of days between any given date and the origin date of January 1, 1970 (often called “day 0” in R and many other programming languages). You enter a date, and you get back the integer number of days since that origin, which is exactly how R stores dates internally when you use the Date class.

Formula

The formula is straightforward once you know the convention: Days since 0 = (target date) − (origin date), where both dates are converted to their internal numeric representation. In R, the origin is fixed at 1970-01-01, and each day adds exactly 1 to the numeric value. So the formula in plain terms is:

days_since_0 = as.numeric(target_date) − as.numeric(origin_date)

where:

  • target_date – the date you want to measure (in YYYY-MM-DD format)
  • origin_date – always 1970-01-01 for R’s Date class
  • days_since_0 – the integer result, which can be negative for dates before 1970

In practice, in R you don’t even need to subtract manually. The function as.numeric(as.Date("2025-06-15")) already returns the number of days since 1970-01-01. The subtraction in the calculator above is shown for clarity, but the result is identical.

Worked Example

Using the default input of 2025-06-15:

  1. Convert the target date to its numeric value: as.numeric(as.Date("2025-06-15")) = 20249 (this is the count of days from 1970-01-01 to 2025-06-15).
  2. Convert the origin date: as.numeric(as.Date("1970-01-01")) = 0.
  3. Subtract: 20249 − 0 = 20249.

So the calculator returns 20249 days since day 0. You can verify this in R by typing as.numeric(as.Date("2025-06-15")) and getting the same number.

Why R Uses 1970-01-01 as Day 0

The choice of January 1, 1970 is not arbitrary. It is the Unix epoch, the reference point used by the POSIX operating system standard that most modern systems (Linux, macOS, and many servers) follow. When R was developed, it adopted this same origin for its Date and POSIXct classes so that dates could be stored as simple integers or doubles, making arithmetic fast and memory-efficient. A single integer takes far less space than a full date string, and operations like finding the difference between two dates become trivial subtraction.

This convention is shared by many other languages and tools, including Python’s datetime module (though Python counts seconds, not days, for its timestamp() method) and JavaScript’s Date object (which counts milliseconds). Knowing this helps when you move data between systems: a date stored as “days since 1970” in R can be converted to a Unix timestamp by multiplying by 86400 (seconds per day).

How to Read Negative Values

If you enter a date before 1970-01-01, the calculator returns a negative number. For example, as.numeric(as.Date("1960-01-01")) returns −3653. This is perfectly valid in R; it simply means the date is 3653 days before the origin. Many beginners are surprised by this, but it is the correct behavior and allows R to handle historical dates without any special casing. When you print such a date, R converts the negative integer back to a readable calendar date, so you never see the raw number in normal use.

One practical implication: if you are sorting dates or finding ranges, negative values work exactly like positive ones. The smallest (most negative) number is the earliest date, and the largest is the latest. There is no need to add a constant offset unless you are exporting to a system that cannot handle negative numbers.

Common Mistakes When Working with Days in R

Several pitfalls trip up even experienced R users:

  • Confusing Date and POSIXct: Date stores whole days, while POSIXct stores seconds since 1970-01-01 00:00:00 UTC. If you use as.numeric() on a POSIXct object, you get seconds, not days. Always check the class first with class(your_object).
  • Time zone issues: When you convert a character string to a date with as.Date(), R assumes UTC by default. But if you use as.POSIXct(), the time zone matters, and a date like “2025-06-15” can become 2025-06-14 in some zones if you are not careful. For pure date arithmetic, stick with as.Date().
  • Leap years: R’s Date class handles leap years automatically. The number of days between 2020-02-28 and 2020-03-01 is 2, not 3, because 2020 is a leap year. Do not try to manually add 365 days to a date; use seq() or lubridate::years() instead.
  • Formatting errors: R expects dates in YYYY-MM-DD format by default. If you enter “06/15/2025”, R will either error or misinterpret it as the 6th day of the 15th month, which does not exist. Always use the ISO 8601 format or specify format = "%m/%d/%Y".

Practical Uses of Days Since 0

The day-count representation is not just an internal detail; it is genuinely useful in data analysis:

  • Time series modeling: Many regression and machine learning models require numeric inputs. Using days since 0 as a feature lets you model trends, seasonality, and growth over time without parsing date strings.
  • Calculating age in days: Subtract a birth date from today’s date using as.numeric(Sys.Date() - as.Date("1990-05-20")) to get an exact day count.
  • Finding intervals between events: If you have a column of dates, diff(as.numeric(dates)) gives you the gaps between consecutive events in days.
  • Merging datasets: When joining two tables on a date, converting to an integer key can sometimes speed up the merge and avoid format mismatches.

Remember that the result is always an integer for Date objects, because R truncates any time-of-day component when you use as.Date(). If you need fractional days (for example, 12.5 days), you must work with POSIXct and divide seconds by 86400.

Alternatives and Related Functions

While as.numeric(as.Date()) is the direct way to get days since 0, R offers several other date-related tools that you might encounter:

  • lubridate::ymd() – parses dates and returns a Date object; then as.numeric() works the same way.
  • difftime() – calculates the difference between two dates and lets you specify the unit (days, weeks, hours). It returns a difftime object, not a plain number.
  • julian() – returns the number of days since 1970-01-01 as well, but it also includes a time component if you pass a POSIXct object. For dates, it is equivalent to as.numeric().
  • as.Date("1970-01-01") + 20249 – the reverse operation: adding days to the origin gives you the calendar date. This is a quick way to verify your calculation.

If you are working with dates before 1582 (when the Gregorian calendar was introduced), R will still calculate days, but the historical accuracy of the calendar conversion may be questioned. For most modern applications this is irrelevant.

Frequently Asked Questions

Why does my R code return a large number like 20249 instead of a date?

Because you are calling as.numeric() on a Date object. That function strips away the date formatting and reveals the underlying integer count of days since 1970-01-01. To see the date again, use as.Date(20249, origin = "1970-01-01").

Is day 0 the same in Excel?

No. Excel uses January 1, 1900 as its day 1 (with a known bug that treats 1900 as a leap year). So a date that is 20249 in R would be approximately 45658 in Excel’s serial number system. Always convert explicitly when moving data between R and Excel.

Can I use this for dates before 1970?

Yes. R handles negative day counts without issue. For example, as.numeric(as.Date("1945-08-15")) returns −8908. This is correct and useful for historical analyses.

What about time zones when calculating days?

For Date objects, time zones do not matter because a Date has no time component. However, if you convert to POSIXct and then to days, the time zone can shift the result by one day. To avoid this, always use as.Date() for day-based arithmetic.

How do I get today’s days since 0 in R?

Simply run as.numeric(Sys.Date()). The function Sys.Date() returns today’s date, and as.numeric() converts it to the day count. This is a common idiom in scripts that need a numeric “today” value.

Does the result include leap seconds?

No. R’s Date class counts whole calendar days, not seconds. Leap seconds are ignored entirely, which is fine for almost all practical purposes because a day is defined as 24 hours in R’s internal representation.

Related

Formula used
in date | Date (YYYY-MM-DD) | - | 2025-06-15 | 1970-01-01 | 2100-12-31
out days | Days since 1970-01-01 | days | (as.numeric(as.Date(date)) - as.numeric(as.Date("1970-01-01"))) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More on this panel

The whole Numbers and Counting panel →