Python DateTime Tutorial with Examples

Python DateTime Tutorial:

In our previous tutorial, we discussed Python OOPs concept in detail.

We learned more about class, objects, constructor and oops along with the different pillars of oops like Inheritance, Overloading, Overriding, and Data hiding that are supported by Python.

In this tutorial will discuss an additional python concept which is Python DateTime. Read through our Series of Python Tutorials for better understanding of Python concepts in detail.

Python DateTime

Watch the VIDEO Tutorial

A Detailed Look at Python DateTime:

Python DateTime

In Python, date, time and DateTime are inbuilt classes which provide us with a number of inbuilt functions to deal with DateTime.

These functions are used to get the current date, time and day.

Let's see some of the examples for all the above.

Example 1:

 
from datetime import date

def test_date():
       today = date.today()
       print(“Today’s date is”, today)

test_date() 

Output:

Today’s date is 2018-09-29

Python date_time_example_1

Output:

date_time_exmaple_1_output

Example 2:

 
from datetime import date

def test_date():
       today = date.today()
      #To print individual date componets
       print(“Date components are:”, today.day, today.month, today.year)

test_date() 

Output:

Date components are: 29 9 2018

Python - date_time_example_2

Output:

date_time_exmaple_2_output

Example 3:

 
from datetime import date

def test_date():
       today = date.today()
      #To print the weekday number(0=Monday , 6=Sunday)
      print(“Weekday number is:”, today.weekday())

test_date() 

Output:

Weekday number is: 5

Python - date_time_example_3

Output:

date_time_exmaple_3_output

Example 4:

 
from datetime import datetime

def test_date():
       today = datetime.now()
       #Print the curren date and time
       print(“Current date and time is:”, today)

test_date() 

Output:

Current date and time is: 2018-09-29 21:26:09.578260

Python - date_time_example_4

Output:

date_time_exmaple_4_output

Example 5:

 
from datetime import datetime

def test_date():
       time = datetime.time(datetime.now())
      #to retrieve the current time
      print(“Current time is:”, time)

test_date() 

Output:

Current time is: 21:28:32.980759

Python - date_time_example_5

Output:

date_time_exmaple_5_output

Formatting date and time using strftime() method

Example 6:

 
import datetime
print(“Current date and time is:”, datetime.datetime.now())
print(“Current date and time using strftime method:”, datetime.datetime.now().strftime(“%y-%m-%d-%H-%M”)
print(“Current year is:”, datetime.date.today().strftime(“%Y”))
print(“Month of year is:”, datetime.date.today().strftime(“%B”))
print(“Week number of the year is:”, datetime.date.today().strftime(“%W”))
print(“Weekday of the week is:”, datetime.date.today().strftime(“%w”))
print(“Day of the year is:”, datetime.date.today().strftime(“%j”))
print(“Day of the month is:”, datetime.date.today().strftime(“%d”))
print(“Day of the week is:”, datetime.date.today().strftime(“%A”)) 

Output:

Current date and time is: 2018-09-29 21:32:30.643372
Current date and time using the strftime method: 18-09-29-21-32
The current year is: 2018
Month of year is: September
Week number of the year is: 39
Weekday of the week is: 6
Day of the year is: 272
Day of the month is: 29
Day of the week is: Saturday

Python - date_time_example_6

Output:

date_time_exmaple_6_output

Conclusion

In this tutorial we discussed some of the additional topics of string in-built functions, and other concepts like opening and deleting a file and inbuilt methods provided by DateTime in detail.

Our upcoming tutorial will explain about Python String functions in detail!!

 PREV Tutorial | NEXT Tutorial