Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Python DateTime – DateTime Class

  • Last Updated : 19 Aug, 2021

DateTime class of the DateTime module as the name suggests contains information on both date as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike date class, the objects of DateTime class are potentially aware objects i.e. it contains information regarding time zone as well.


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

The year, month, and day arguments are mandatory. tzinfo can be None, rest all the attributes must be an integer in the following range –  

  • MINYEAR(1) <= year <= MAXYEAR(9999)
  • 1 <= month <= 12
  • 1 <= day <= number of days in the given month and year
  • 0 <= hour < 24
  • 0 <= minute < 60
  • 0 <= second < 60
  • 0 <= microsecond < 1000000
  • fold in [0, 1]

Note: Passing an argument other than integer will raise a TypeError and passing arguments outside the range will raise ValueError.

Example: Creating an instance of DateTime class


# Python program to
# demonstrate datetime object
from datetime import datetime
# Initializing constructor
a = datetime(2022, 10, 22)
# Initializing constructor
# with time parameters as well
a = datetime(2022, 10, 22, 6, 2, 32, 5456)
2022-10-22 00:00:00
2022-10-22 06:02:32.005456

Class Attributes

Let’s see the attributes provided by this class – 

Attribute NameDescription
minThe minimum representable DateTime
maxThe maximum representable DateTime
resolutionThe minimum possible difference between datetime objects
yearThe range of year must be between MINYEAR and MAXYEAR
monthThe range of month must be between 1 and 12
dayThe range of day must be between 1 and number of days in the given month of the given year
hourThe range of hour must be between 0 and 24 (not including 24)
minuteThe range of minute must be between 0 and 60 (not including 60)
secondThe range of second must be between 0 and 60 (not including 60)
microsecondThe range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfoThe object containing timezone information
foldRepresents if the fold has occurred in the time or not

Example 1: Getting the minimum and maximum representable DateTime object


from datetime import datetime
# Getting min datetime
mindatetime = datetime.min
print("Min DateTime supported", mindatetime)
# Getting max datetime
maxdatetime = datetime.max
print("Max DateTime supported", maxdatetime)
Min DateTime supported 0001-01-01 00:00:00
Max DateTime supported 9999-12-31 23:59:59.999999

Example 2: Accessing the attributes of date and time object


from datetime import datetime
# Getting Today's Datetime
today =
# Accessing Attributes
print("Day: ",
print("Month: ", today.month)
print("Year: ", today.year)
print("Hour: ", today.hour)
print("Minute: ", today.minute)
print("Second: ", today.second)
Day:  26
Month:  7
Year:  2021
Hour:  16
Minute:  24
Second:  7

Class Functions

The DateTime class provides various functions to deal with the DateTime objects like we can convert DateTime object to string and string to DateTime objects, we can also get the weekday for the particular day of the week of the particular month, we can also set the time zone for a particular DateTime object, etc.

List of DateTime class Methods

Function NameDescription
astimezone()Returns the DateTime object containing timezone information.
combine()Combines the date and time objects and return a DateTime object
ctime()Returns a string representation of date and time
date()Return the Date class object
fromisoformat()Returns a datetime object from the string representation of the date and time
fromordinal()Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. The hour, minute, second, and microsecond are 0
fromtimestamp()Return date and time from POSIX timestamp
isocalendar()Returns a tuple year, week, and weekday
isoformat()Return the string representation of date and time
isoweekday()Returns the day of the week as integer where Monday is 1 and Sunday is 7
now()Returns current local date and time with tz parameter
replace()Changes the specific attributes of the DateTime object
strftime()Returns a string representation of the DateTime object with the given format
strptime()Returns a DateTime object corresponding to the date string
time()Return the Time class object
timetuple()Returns an object of type time.struct_time
timetz()Return the Time class object
today()Return local DateTime with tzinfo as None
toordinal()Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1
tzname()Returns the name of the timezone
utcfromtimestamp()Return UTC from POSIX timestamp
utcoffset()Returns the UTC offset
utcnow()Return current UTC date and time
weekday()Returns the day of the week as integer where Monday is 0 and Sunday is 6

Example 1: Getting Today’s Date


from datetime import datetime
# Getting Today's Datetime
today =
print("Today's date using now() method:", today)
today =
print("Today's date using today() method:", today)


Today’s date using now() method: 2021-07-26 22:23:22.725573

Today’s date using today() method: 2021-07-26 22:23:22.725764

Example 2: Getting DateTime from timestamp and ordinal


from datetime import datetime
# Getting Datetime from timestamp
date_time = datetime.fromtimestamp(1887639468)
print("Datetime from timestamp:", date_time)
# Getting Datetime from ordinal
date_time = datetime.fromordinal(737994)
print("Datetime from ordinal:", date_time)
Datetime from timestamp: 2029-10-25 16:17:48
Datetime from ordinal: 2021-07-23 00:00:00

Note: For more information on Python Datetime, refer to Python Datetime Tutorial

My Personal Notes arrow_drop_up
Recommended Articles
Page :