Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables


Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators


Python - If Else statement


Python - While LoopPython - For Loop


Python - NumbersPython - Number Conversion


Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method


Python - ListPython - List Methods


Python - Tuple


Python - SetPython - Set Methods


Python - DictionaryPython - Dictionary Methods


Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables


Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module


Python - Read input from keyboard


Python - File Handling

Exception Handling

Python - Exception Handling


Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - datetime Module


python logo

In this tutorial we will learn about datatime module in Python.

We learned about modules in the Python - Modules tutorial. Feel free to check that out.

Importing the datetime module

To work with data and time in Python we can take help of the datetime module.

To import the module we write the following.

import datetime

Display the current date and time

We can write the following Python code to display the current date and time using the datetime module.

# import module
import datetime

# current date time
currDT =

# output

The above Python code will give us the following output.

2018-10-21 04:43:58.846551

The date time is in the following format.

Year-Month-Day Hour:Minute:Second.Microsecond

Create date object using datetime() method

In the following Python program we are creating date object for the date 1st Jan 2000 using the datetime module.

# import module
import datetime

# create date
date = datetime.datetime(2000, 1, 1)

# output

The above code will print the following output.

2000-01-01 00:00:00

In the following Python program we are creating a date object for the date time 1st Jan 2000 10:20:30.

# import module
import datetime

# create date time
date = datetime.datetime(2000, 1, 1, 10, 20, 30)

# output

The above code will give the following output.

2000-01-01 10:20:30

The strftime method

We use the strftime() method to convert the date object into human readable string.

The strftime method takes one argument, the format.

In the following Python program we are printing the current date in human readable format using the strftime method.

# import module
import datetime

# current time
currDT =

# output
print("Day", currDT.strftime('%a'))
print("Date", currDT.strftime('%d'))
print("Month", currDT.strftime('%b'))
print("Year", currDT.strftime('%Y'))
print("Hour", currDT.strftime('%H'))
print("Min", currDT.strftime('%M'))
print("Sec", currDT.strftime('%S'))

The above code will give us a similar output.

Day Sun
Date 21
Month Oct
Year 2018
Hour 07
Min 34
Sec 58

Format for the strftime method

%aName of the weekday in short form.Sun
%AFull name of the weekday.Sunday
%wWeekday in number form 0-6.
Sunday = 0
Monday = 1
Saturday = 6
%dDay of the month from 01-31.01
%bName of the month in short form.Oct
%BFull name of the month.October
%mMonth in number form 01-12.
January = 01
February = 02
December = 12
%yShort form year without century.20
%YFull year.2020
%HHour in 24-hour format 00-23.20
%IHour in 12-hour format 00-12.10
%MMinute 00-59.20
%SSecond 00-59.30
%fMicrosecond 000000-999999.102030
%pThis represents the AM/PM.AM
%zUTC offset+0530
%jDay number of year 001-366.
1st Jan = 001
31st Dec = 365
For leap year, 31st Dec = 366
%UWeek number of year 00-53.
Sunday as the first day of the week.
%WWeek number of year 00-53.
Monday as the first day of the week.
%cLocale date and time.Sat Jan 1 10:20:30 2000
%xLocale date.01/01/00
%XLocale time.10:20:30
%%The % character.%