The Ultimate Guide to Python Date and Time

Here are the top examples to get Date and Time in Python. Python has four Date and Time classes Viz: Date, Time, Datetime, and TimeDelta.

Python Datetime

Python datetime

Here are four classes of python datetime.

  1. Date
  2. Time
  3. Timedelta
  4. Datetime

1. Python Date class

The Date class is datetime.date

import datetime
date1 = datetime.date(2020, 7, 23)
print(date1)

The output will be:

2020-07-23

2. Python Time class

The Time class is datetime.time

import datetime
t=datetime.time(12, 10, 30)
print(t)

The output will be:

12:10:30

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Spotlight

The difference between datetime.today and datetime.now is the today will give today’s date, time and timezone. The now will give only date and time.

3. Python Timedelta class

The Timedelta class is datetime.timedelata

import datetime
t=datetime.timedelta(1)
print(t)

The output will be:

1 day, 0:00:00

** Process exited - Return Code: 0 **
Press Enter to exit terminal

4. Python Datetime

The datetime class is datetime.datetime

import datetime
t=datetime.datetime.today()
print(t)

The output will be:

2022-12-25 09:17:10.201894

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Here are the top examples of python datetime classes

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.