Dates & Times with datetime64

np.datetime64 is NumPy's native date-and-time type that stores each instant as an integer number of time units, letting whole arrays of dates take part in fast vectorized arithmetic.

Learn Dates & Times with datetime64 in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll create dates and durations, choose units like 'D', 'h', and 's', do date arithmetic, generate date ranges with np.arange, convert to and from strings, and count business days with np.busday_count.

Build a date by passing an ISO string to np.datetime64("2026-06-17") . NumPy infers the unit from how much detail you give: a bare date gets day resolution 'D' , while including a time gets finer units. A duration is a np.timedelta64(value, unit) , for example five days or two hours.

Subtracting two dates yields a timedelta64 — the gap between them. Adding a timedelta to a date shifts it. And because dates are integers internally, np.arange(start, stop, step) works directly on them to build an evenly spaced range of dates, stepping by whatever unit you provide.

Convert a datetime64 to a string with np.datetime_as_string , and parse strings back by constructing np.datetime64 or using .astype . You can also change resolution by casting between units. Finally, np.busday_count(start, end) counts Monday-to-Friday days in the half-open range, ignoring weekends.

Shift a launch date forward by 30 days by filling in the unit code for days.

Answer: D for days. Other unit codes include 'h' hours, 'm' minutes, and 's' seconds.

A bare number has no unit, so NumPy cannot tell days from seconds:

The range is half-open, so the end date itself is not counted.

✅ Fix: if you want the end day included, add one day to the stop date before counting.

Comparing a datetime64[D] with a datetime64[s] casts to the finer unit, which can confuse equality checks.

✅ Fix: cast both to the same unit with .astype("datetime64[D]") before comparing.

Given a start date and a list of task durations in days, compute each task's end date and the total weekday span.

Lesson complete — NumPy can keep time!

You can create dates and durations, pick a unit, do vectorized date arithmetic, build date ranges with np.arange , convert to and from strings, and count weekdays with np.busday_count .

🚀 Up next: Polynomials — fit and evaluate curves with np.polyfit and np.poly1d .

Practice quiz

What does np.datetime64('2026-01-08') - np.datetime64('2026-01-01') return?

  • 8 days
  • 7 days
  • 1 days
  • 0 days

Answer: 7 days. Subtracting two dates gives a timedelta64; Jan 8 minus Jan 1 is 7 days.

What is the dtype of np.datetime64('2026-06-17')?

A bare date with no time gets day resolution, datetime64[D].

How does NumPy store a datetime64 value internally?

  • As a formatted string
  • As a Python datetime object
  • As an integer count of its unit since 1970-01-01
  • As a float number of seconds

Answer: As an integer count of its unit since 1970-01-01. It is an integer count of its unit since the epoch 1970-01-01, which makes arithmetic fast.

What does np.timedelta64(5, 'D') represent?

  • A duration of 5 days
  • The date 2005
  • 5 hours
  • May 1970

Answer: A duration of 5 days. timedelta64(value, unit) is a duration; 'D' is the day unit, so this is 5 days.

What does np.busday_count('2026-06-15', '2026-06-22') return?

  • 7
  • 6
  • 4
  • 5

Answer: 5. It counts Mon-Fri in the half-open range; that week has 5 weekdays.

Which unit code means hours?

  • 'h'
  • 'H'
  • 'hr'
  • 'hour'

Answer: 'h'. NumPy uses lowercase 'h' for hours; 'm' is minutes and 's' is seconds.

Is the range for np.busday_count(a, b) inclusive of b?

  • Yes, b is always counted
  • No, the range is half-open and excludes b
  • Only if b is a weekday
  • Only if b is a weekend

Answer: No, the range is half-open and excludes b. Like Python's range, it includes a but excludes b.

What does np.datetime64('2026-06-17').astype('datetime64[M]') give?

  • 2026-06-17
  • 2026-06
  • 2026
  • 06-17

Answer: 2026-06. Casting to month resolution drops the day, leaving 2026-06.

How do you add 30 days to a date safely?

  • date + 30
  • date + '30D'
  • date + np.timedelta64(30, 'D')
  • date.add(30)

Answer: date + np.timedelta64(30, 'D'). A timedelta64 with an explicit unit makes the intent clear and unambiguous.

What does np.timedelta64(2, 'D') + np.timedelta64(3, 'D') equal?

  • 6 days
  • 5 days
  • 1 days
  • 23 days

Answer: 5 days. Durations with the same unit add directly: 2 + 3 = 5 days.