Formatting date objects into strings Part - 1

Formatting date objects into strings Part - 1

Lesson Details:
June 29, 2020

I: Introduction

Python is an interpreted, interactive, object-oriented, high-level computer programming language. Python provides constructs that enable clear program specification, was designed to foster code readability.

It is cross platform, portable and easy to learn. It’s free and open source software

II: Body

A: Formatting date objects into strings part - 1

1) To represent the date, there is builtin object datetime.datetime in python which can be initialized in following way:

my_date = datetime.datetime(2016, 10, 16)

2) Using datetime.datetime object, we can create different objects depending on the requirement of the task. For example, to get year, month or day information, we can use following commands:

y_num = my_date.year

m_num = my_date.month

d_num =

3) We can also convert date to string format in two ways:

The first method is by using strftime() function which helps us to convert date to string in specific format.

strftime(‘%Y-%m-%d’, my_date) will give output as ‘2016-10-16’. We can use different format codes as per our requirements. See table below for a list of format codes:

Code Output %Y 2016 %m 10

Course content