NumPy: Date and Time

Last updated: 2026-08-26

1. What You'll Learn



2. Key Concepts

PYTHON
import numpy as np

# Creating datetime arrays
dates = np.array(['2024-01-01', '2024-02-01', '2024-03-01'], dtype='datetime64')
print(dates)  # ['2024-01-01' '2024-02-01' '2024-03-01']

# Date arithmetic
print(dates + np.arange(3))  # add days

# Date range
range_dates = np.arange('2024-01-01', '2024-01-10', dtype='datetime64')
print(range_dates)

# Extract components
print(dates.astype('datetime64[Y]'))  # year
print(dates.astype('datetime64[M]'))  # month
TEXT 📖 Display only
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

▶ Example: Creating date ranges (Difficulty ⭐)

PYTHON
import numpy as np

# Daily range
days = np.arange('2024-01-01', '2024-01-11', dtype='datetime64')
print("Daily:", days)

# Monthly range
months = np.arange('2024-01', '2024-07', dtype='datetime64[M]')
print("Monthly:", months)

# Hourly range
hours = np.arange('2024-01-01', '2024-01-02', dtype='datetime64[h]')
print("Hourly:", hours)

Output:

TEXT 📖 Display only
Daily: ['2024-01-01' '2024-01-02' '2024-01-03' '2024-01-04' '2024-01-05'
 '2024-01-06' '2024-01-07' '2024-01-08' '2024-01-09' '2024-01-10']
Monthly: ['2024-01' '2024-02' '2024-03' '2024-04' '2024-05' '2024-06']
Hourly: ['2024-01-01T00' '2024-01-01T01' '2024-01-01T02' '2024-01-01T03'
 '2024-01-01T04' '2024-01-01T05' '2024-01-01T06' '2024-01-01T07'
 '2024-01-01T08' '2024-01-01T09' '2024-01-01T10' '2024-01-01T11'
 '2024-01-01T12' '2024-01-01T13' '2024-01-01T14' '2024-01-01T15'
 '2024-01-01T16' '2024-01-01T17' '2024-01-01T18' '2024-01-01T19'
 '2024-01-01T20' '2024-01-01T21' '2024-01-01T22' '2024-01-01T23']

▶ Example: Date arithmetic (Difficulty ⭐⭐)

PYTHON
import numpy as np

start = np.array('2024-01-15', dtype='datetime64')

# Add days
print("Start:", start)
print("+7 days:", start + 7)
print("+30 days:", start + 30)
print("-5 days:", start - 5)

# Difference between dates
d1 = np.array('2024-01-01', dtype='datetime64')
d2 = np.array('2024-12-31', dtype='datetime64')
delta = d2 - d1
print("Days in 2024:", delta)

# Extract components
date = np.array('2024-07-04', dtype='datetime64')
print("Year:", date.astype('datetime64[Y]'))
print("Month:", date.astype('datetime64[M]'))

Output:

TEXT 📖 Display only
Start: 2024-01-15
+7 days: 2024-01-22
+30 days: 2024-02-14
-5 days: 2024-01-10
Days in 2024: 365 days
Year: 2024
Month: 2024-07

▶ Example: Business day calculations (Difficulty ⭐⭐)

PYTHON
import numpy as np

start = np.datetime64('2024-01-01')
end = np.datetime64('2024-01-31')

# Count business days
bd = np.busday_count(start, end)
print(f"Business days Jan 2024: {bd}")

# Find next business day
print("Next business day after 2024-01-05:", np.busday_offset('2024-01-05', 0, roll='forward'))

# Business day range
biz_days = np.busday_offset('2024-01-01', np.arange(10))
print("First 10 business days of 2024:", biz_days)

Output:

TEXT 📖 Display only
Business days Jan 2024: 22
Next business day after 2024-01-05: 2024-01-08
First 10 business days of 2024: ['2024-01-01' '2024-01-02' '2024-01-03' '2024-01-04' '2024-01-05'
 '2024-01-08' '2024-01-09' '2024-01-10' '2024-01-11' '2024-01-12']

Q What's the difference between datetime64 and Python's datetime?
A datetime64 is a fixed-width 64-bit integer representing time, making it fast and memory-efficient. Python's datetime is a full object with more flexibility but higher overhead.
Q What's the resolution of datetime64?
A It depends on the unit — datetime64[Y] (year), datetime64[M] (month), datetime64[D] (day), datetime64[h] (hour), datetime64[ms] (millisecond), datetime64[ns] (nanosecond).
Q How do I compute business days?
A Use np.busday_count(start, end) to count business days between two dates, and np.busday_offset to find the nearest business day.

❓ FAQ

Q What is the most important thing to remember?
A NumPy operations are vectorized — avoid Python loops for better performance.
Q Where can I learn more?
A Check the official NumPy documentation at numpy.org for detailed references and advanced topics.
Q Does this work with NumPy 2.x?
A Yes — all examples are compatible with NumPy 2.x. Some older APIs (like np.random.seed) are still supported but the modern alternatives are recommended.

📖 Summary



📝 Exercises

  1. Beginner (Difficulty ⭐): Create an array of all dates in January 2024. Print the first 5 and last 5 dates.

  2. Intermediate (Difficulty ⭐⭐): Create a date range from 2024-01-01 to 2024-12-31. Extract the month from each date and count how many dates fall in each month.

  3. Advanced (Difficulty ⭐⭐⭐): Compute the number of business days between 2024-01-01 and 2024-12-31. Compare with the total number of calendar days.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏