Python Datetime

Python Datetime

Python Dates

A Python date isn't a data type of itself, but a module named datetime is available for import for dates as date objects.

Example :- Import module datetime and show current date :

import datetime
x =

Output :-

2021-08-09 18:24:37.522803

Date includes year, month, day, hour, minute, second and microsecond.

The datetime module contains various methods for returning date object information.

Example :- year and weekday name and returned :

import datetime
x =

Output :-


You can also search for these topics, python date output format, define the python date output greater than, how to generate the output of python date, python print date without time, python print date with format, python date output format, example for python date output.

Creating Date Objects

We can use the datetime() class (builder) of the datatime module to produce a date.

The datetime() class needs a date with 3 parameters : year, month, day.

Example :- Create a date object :

import datetime
x = datetime.datetime(2020, 5, 17)

Output :-

2020-05-17 00:00:00

The datetime() class requires time and timezone (time, minute, second, microsecond, tzone), however it is optional with a default 0. (None for timezone).

You can also search for these topics, create date object python from string, create empty date object python, python create date objects copy, custom date objects to create python, how to use counter python date objects, example for python create date objects, creating date objects key value in python.

The strftime() Method

The datetime object has a formatted date objects method in readable strings.

The method is called strftime() and requires one parameter, format, to indicate the format of the string returned :

Example :- Display the name of the month :

import datetime
x = datetime.datetime(2018, 6, 1)

Output :-


All the legal format codes are referred to :

Directive Description Example
%a Weekday, short version Wed
%A Weekday, full version Wednesday
%w Weekday as a number 0-6, 0 is Sunday 3
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version December
%m Month as a number 01-12 12
%y Year, short version, without century 18
%Y Year, full version 2018
%H Hour 00-23 17
%I Hour 00-12 05
%M Minute 00-59 41
%S Second 00-59 08
%f Microsecond 000000-999999 548513
%z UTC offset +0100
%Z Timezone CST
%j Day number of year 001-366 365
%U Week number of year, Sunday as the first day of week, 00-53 52
%W Week number of year, Monday as the first day of week, 00-53 52
%c Local version of date and time Mon Dec 31 17:41:00 2018
%x Local version of date 12/31/18
%X Local version of time 17:41:00
%% A % character %
%G ISO 8601 year 2018
%u ISO 8601 weekday(1-7) 1
%V ISO 8601 weeknumber (01-53) 01

You can also search for these topics, python the strftime example, method alternative to strftime() function, python how to use the comparison of strftime(), python change language of strftime(), format the python strftime() method, python to define the strftime() error, define the python date directives and description.