Python DateTime - DateTime Class - GeeksforGeeks

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.
Syntax:
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
Python3
# Python program to# demonstrate datetime object  from datetime import datetime  # Initializing constructora = datetime(2022, 10, 22)print(a)  # Initializing constructor# with time parameters as wella = datetime(2022, 10, 22, 6, 2, 32, 5456)print(a)
2022-10-22 00:00:00
2022-10-22 06:02:32.005456
Let’s see the attributes provided by this class – 
Example 1: Getting the minimum and maximum representable DateTime object
Python3
from datetime import datetime  # Getting min datetimemindatetime = datetime.minprint("Min DateTime supported", mindatetime)  # Getting max datetimemaxdatetime = datetime.maxprint("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
Python3
from datetime import datetime  # Getting Today's Datetimetoday = datetime.now()  # Accessing Attributesprint("Day: ", today.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
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.
Example 1: Getting Today’s Date
Python3
from datetime import datetime  # Getting Today's Datetimetoday = 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: 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
Python3
from datetime import datetime  # Getting Datetime from timestampdate_time = datetime.fromtimestamp(1887639468)print("Datetime from timestamp:", date_time)  # Getting Datetime from ordinaldate_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
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Selecting rows in pandas DataFrame based on conditions
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Python | Get unique values from a list
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()