Free cookie consent management tool by TermsFeed The combine method of datetime class in Python | Pythontic.com

The combine method of datetime class in Python

Overview:

  • The combine() class method of datetime class creates a datetime object by combining a date and time

  • The combine() method crafts a datetime object by summarizing the date and time information from two different date and time objects.

  • For timezone information the combine() method relies on time object that is passed as the parameter. If the time object passed in does not have timezone object then no timezone is assumed.

  • If a datetime object is passed for the date object it ignores the timezone info from this parameter. If a datetime object is passed for the time object, the timezone is set accordingly.

Example:

# Example Python program that creates 
# datetime by combining date and time
# objects through the class method combine()
# of the datetime class
import datetime as dt

# Create a date object
dateOb = dt.date(2025, 8, 30) # Year, month, day

# Create a time object
timeOb = dt.time(11, 12, 23)  # Hour, minute, second

# Create a datetime object
dateTimeOb = dt.datetime.combine(dateOb, timeOb)
print("Date and time:")
print(dateTimeOb)

Output:

Date and time:
2025-08-30 11:12:23

 


Copyright 2025 © pythontic.com