AppDividend
Latest Code Tutorials

Python Datetime Example | Basic Date and Time Types Tutorial

0

Python Datetime Example | Basic Date and Time Types Tutorial is today’s topic. In this article, you will learn to display and manipulate date and time in Python with the help of examples. Python has a module named datetime to work with dates and times. The date in Python is not the data of its own, but we can import the module named datetime to work with dates as date objects.

Python Datetime Example

Let’s display the current date in Python Console. Commonly used classes in the datetime module are following.

  • date Class
  • time Class
  • datetime Class
  • timedelta Class

Get Current Date in Python

Create a new file called app.py and write the following code inside the app.py file.

# app.py

from datetime import datetime

now = datetime.now()
print(now.date())

First, we have imported the datetime module and used the object datetime from the datetime module.

Then we have called now function on that datetime object, and we get the now object.

Again, we have called another method on now object called date which finally, returns the current date. Run the above file and see the output.

 

Python Datetime Example

Now, let’s display year, day, month. Add the following code inside the app.py file.

# app.py

from datetime import datetime

now = datetime.now()

print(now.date())
print(now.day)
print(now.month)
print(now.year)

In the above code, we have used the property of day, month, and year on now object. See the output below.

 

Get Current Date in Python

Get Current Hour, Minutes, and Seconds in Python

Now, let’s display the current hour, minute, and seconds using the following code.

# app.py

from datetime import datetime

now = datetime.now()

print(now.hour)
print(now.minute)
print(now.second)

See the output below.

 

Get Current Hour, Minutes, and Seconds in Python

We can also print the current time whole using the following code.

# app.py

from datetime import datetime

now = datetime.now()

print(now.time())

See the output below.

 

Get Current Time in Python

So, our whole code looks like below.

# app.py

from datetime import datetime

now = datetime.now()

print(now.date())
print(now.day)
print(now.month)
print(now.year)

print(now.time())
print(now.hour)
print(now.minute)
print(now.second)

From the above code, you can analyze that to get current date and time, we have used the function called date() and time() on now() object.

If we want specific details like which day and which hour then we have used the Attribute of now object.

Creating Date Objects

If we want to construct a date, we can use the datetime()  constructor of the datetime module.

The datetime() class requires mainly three parameters to construct a date, and that is a year, month, day.

See the following example.

# app.py

from datetime import datetime

createdDate = datetime(2020, 1, 1)
print('The newly created date is:', createdDate)

The datetime() class also takes the parameters for time and timezone (hour, minute, second, microsecond, timezone), but they are optional and has a default value of 0, (None for timezone).

See the below output.

 

Creating Date Objects

In the above code, timestamp has been displayed. We can also print the plain date using the following code.

# app.py

import datetime

createdDate = datetime.date(2020, 1, 1)
print('The newly created date is:', createdDate)

See the below output.

 

Creating Date Objects in Python

datetime.date Class in Python

You can instantiate date objects from the date class. A date object represents a date (year, month and day).

Let’s get the current date using the date object.

# app.py

from datetime import date

today = date.today()
print("Current date:", today)

See the below output.

 

date Class in Python

Get today’s year, month and day using a date object

Let’s write the code to get today’s year, month and day using the date object.

# app.py

from datetime import date

print("Current date:", today)
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)

Run the file and see the output.

 

Get today's year, month and day using a date object

datetime.time Class in Python

The time object instantiated from the time class represents the local time.

# app.py

from datetime import time

t = time()
print("time is:", t)

tm = time(12, 19, 18)
print("The time is =", tm)

ts = time(hour = 11, minute = 30, second = 46)
print("Time is =", ts)

ti = time(12, 21, 19, 192118)
print("Time is:", ti)

In the above example, we have constructed the time providing the required parameters. See the below output. 

 

time Class in Python

Finally, Python Datetime Example | Basic Date and Time Types Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.