DateTime class of the DateTime module as the name suggests contains information on both dates 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 the date class, the objects of the DateTime class are potentially aware objects i.e. it contains information regarding time zone as well.
Syntax of Python DateTime
The syntax of the Python DateTime class is as follows:
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. The tzinfo can be None, and the 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 an integer will raise a TypeError and passing arguments outside the range will raise ValueError.
Example: Creating an instance of the DateTime class
Python3
# Python program to # demonstrate datetime object from datetime import datetime # Initializing constructor a = datetime( 2022 , 10 , 22 ) print (a) # Initializing constructor # with time parameters as well a = datetime( 2022 , 10 , 22 , 6 , 2 , 32 , 5456 ) print (a) |
Output:
2022-10-22 00:00:00
2022-10-22 06:02:32.005456
Class Attributes of DateTime
Let’s see the attributes provided by this class:
Attribute Name |
Description |
---|---|
min |
The minimum representable DateTime |
max |
The maximum representable DateTime |
resolution |
The minimum possible difference between datetime objects |
year |
The range of year must be between MINYEAR and MAXYEAR |
month |
The range of month must be between 1 and 12 |
day |
The range of days must be between 1 and the number of days in the given month of the given year |
hour |
The range of hours must be between 0 and 24 (not including 24) |
minute |
The range of minutes must be between 0 and 60 (not including 60) |
second |
The range of second must be between 0 and 60 (not including 60) |
microsecond |
The range of microseconds must be between 0 and 1000000 (not including 1000000) |
tzinfo |
The object containing timezone information |
fold |
Represents if the fold has occurred in the time or not |
Example: Getting the minimum and maximum representable DateTime object
Python3
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) |
Output:
Min DateTime supported 0001-01-01 00:00:00
Max DateTime supported 9999-12-31 23:59:59.999999
The now() Method
We can get the current date and time using the now() method of the datetime module.
Python3
from datetime import datetime # Getting Today's Datetime today = datetime.now() # Accessing Attributes print ( "Day: " , today.day) print ( "Month: " , today.month) print ( "Year: " , today.year) print ( "Hour: " , today.hour) print ( "Minute: " , today.minute) print ( "Second: " , today.second) |
Output:
Day: 16
Month: 6
Year: 2023
Hour: 5
Minute: 28
Second: 47
DateTime Class Functions
The DateTime class provides various functions to deal with the DateTime objects. We can convert the 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
Let us see a few methods provided by the DateTime class in Python.
Function Name |
Description |
---|---|
astimezone() |
Returns the DateTime object containing timezone information. |
combine() |
Combines the date and time objects and returns a DateTime object |
ctime() |
Returns a string representation of the 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 the date and time |
isoweekday() |
Returns the day of the week as an 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 an integer where Monday is 0 and Sunday is 6 |
Example 1: Getting Today’s Date
Python3
from datetime import datetime # Getting Today's Datetime today = datetime.now() print ( "Today's date using now() method:" , today) today = datetime.today() print ( "Today's date using today() method:" , today) |
Output:
Today's date using now() method: 2023-06-16 05:30:57.195635
Today's date using today() method: 2023-06-16 05:30:57.196321
Example 2: Getting DateTime from timestamp and ordinal.
Python3
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) |
Output:
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