Date and time in Python
Introduction
In this article you will learn how to work with date and time in Python 3.
Current time
Create the file current_time.py with the following contents
import datetime
current_time = datetime.datetime.now()
print(f"Now: {current_time}")
# Hour, minute and second separately
print(f"\nHour: {now.hour}"
f"\nMinute: {now.minute}"
f"\nSecond: {now.second}"
)
# Year, month and day separately
print(f"\nYear: {now.year}"
f"\nMonth: {now.month}"
f"\nDay: {now.day}"
)
python current_time.py
Now: 2026-11-17 21:15:40.185141 Hour: 09 Minute: 15 Second: 40 Year: 2026 Month: 11 Day: 17
Today's date
Create the file today.py with the following contents
import datetime
today = datetime.date.today()
print(f"Today: {today}")
python today.py
Today: 2026-11-17
Working with time in the standard library
Create the file
date_time.py and copy the code from the examples into it.
You can run the file with the command
python3 date_time.py
import datetime # DATE specified_date = datetime.date(2020, 11, 23) print(f"specified_date: {specified_date}") today = datetime.date.today() print(f"today: {today}") print(f"today.year: {today.year}") print(f"today.month: {today.month}") print(f"today.day: {today.day}") # Monday is 0 Sunday is 6 print(f"today.weekday: {today.weekday()}") # Monday is 1 Sunday is 7 print(f"today.isoweekday: {today.isoweekday()}")
specified_date: 2020-11-23 today: 2026-11-17 today.year: 2026 today.month: 11 today.day: 17 today.weekday: 1 today.isoweekday: 2
Time intervals
You can add time intervals to the current time or subtract them from it
import datetime # DATE tdelta = datetime.timedelta(days=7) print(f"next week: {today + tdelta}") print(f"last week: {today - tdelta}")
next week: 2026-11-24 last week: 2026-11-10
bday = datetime.date(2134, 3, 9) till_bday = bday - today print(f"days till 200 birthday: {till_bday.days}") print(f"seconds till 200 birthday: {till_bday.total_seconds()}") # TIME t = datetime.time(9, 30, 45, 100000) print(f"hour: {t.hour}") # DATETIME dt = datetime.datetime(2020, 11, 23, 12, 42, 30, 100000) print(f"time: {dt.time()}") print(f"year: {dt.year}") tdelta = datetime.timedelta(days=7) print(f"dt + delta: {dt + tdelta}") dt_today = datetime.datetime.today() dt_now = datetime.datetime.now() dt_utcnow = datetime.datetime.utcnow() print(f"today: {dt_today}\nnow: {dt_now}\nutc: {dt_utcnow}")
days till 200 birthday: 40851 seconds till 200 birthday: 3529526400.0 hour: 9 time: 21:15:40.100000 year: 2026 dt + delta: 2026-11-24 21:15:40.100000 today: 2026-11-17 21:15:40.764377 now: 2026-11-17 21:15:40.764386 utc: 2026-11-17 19:15:40.764390
Functions from datetime
_is_leap()
Determines whether a year is a leap year
def _is_leap(year): "year -> 1 if leap year, else 0." return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
_days_before_year()
Determines how many days have passed between January 1 of year 1 and January 1 of the given year
def _days_before_year(year): "year -> number of days before January 1st of year." y = year - 1 return y*365 + y//4 - y//100 + y//400
fromtimestamp
To convert time from the
epochtime
format to a regular one, you can use the method
fromtimestamp
We get the time in the
epochtime
format using the method
st_mtime
from the library
pathlib
from pathlib import Path from datetime import datetime with open("cities.txt", "w") as f: f.write("Barcelona, Benalmadena, Cordoba, Malaga, Torremolinos") file = Path("cities.txt") file_stat = file.stat() print(file_stat.st_mtime) print(datetime.fromtimestamp(file_stat.st_mtime))
1794942940.020916 2026-11-17 21:15:40.020916
Article author: Andrei Olegovich