Python Datetime Module

Python Datetime Module:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) The Python datetime module provides a set of functions and methods for creating, manipulating, and formatting datetime objects.

(2) The datetime() method creates a datetime object and accepts the year, month, day, and optionally hour, minute, second, milliseconds, and time zone arguments.

datetime method in python

(3) The datetime.now() method is used to see the current date and time.

datetime.now() method in python

(4) strftime() The strftime() function allows datetime objects to be converted into custom-formatted strings. This function takes a format string as its input. This format string is made up of special characters beginning with the percent sign as shown below.

FormatterDescriptionExample
%bMonth as locale’s abbreviated name.Jan
%BMonth as locale’s full name.January
%mMonth as a zero-padded decimal number.05
%MMinute as a zero-padded decimal number.45
%yYear without century as a zero-padded decimal number.22
%YYear with century as a decimal number.2022
%HHour (24-hour clock) as a zero-padded decimal number.16
%IHour (12-hour clock) as a zero-padded decimal number.08
%pLocale’s equivalent of either AM or PM.PM
strftime function in python
python strftime function

Tutorial Python
Tutorial MySQL
Natural Language Processing (NLP)
What is Virtual Reality?
Spooling and Virtual Devices
Operating System as a Resource Manager
Linux- Features, and Benefits
Python strftime cheatsheet

Comments (No)

Leave a Reply