How to Implement the Python Datetime with Example

Python Datetime

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat.


The Python DateTime and Time

This is the python get current date module of the entire python tutorial. It is important for you to know that in this programming language date is not actually a data type of its own. But if you wish to work with date and time in this programming language then you can do that by using or importing the datetime module. This will allow you to work with specific date related objects and dates. For example, if you wish to display the current date by importing this particular module then


The Date Output

Whenever you choose to run the program of python get current date which we created above then you will observe that the result will be something like what is mentioned below.

2018-09-10 23 : 47 : 45 . 555500

This particular date contains all the related information like the microsecond, second, minute, hour, day, month, and year. This module can also help in returning a number of different information that is related to the object. If you wish to return the name of the weekday and the current year then the python datetime example for this is mentioned below.



To Create Date Objects

While using this programming language if you wish to create a particular date then you can also do that by using the datetime ( ) class constructor that is a part of the datetime module. It is important for you to also be aware of the fact that this particular class has basically three different parameters. And those parameters are the year, month, and date. If you wish to create a particular object then the python datetime example for this is mentioned below



This particular class also has a number of other parameters and those parameters are the second, minute, hour, microsecond, tzone, and many other. All these Python datetime now parameters, are rather optional and if you do not enter a value for these parameters then it will be automatically treated as 0 apart from the tzone which will have a default value of none.


The strftime ( ) Method

While using this programming language if you wish to modify the formatting date objects into some kind of readable string then you do that by using the strftime ( ) method. While using this method, you will be required to pick a particular parameter and then specify the particular format of the string that is returned. For example, if you wish to display the particular name of the month by using strftime python then



For your ease of learning, we have made up a list of all the legal format codes that you will need while using this programming language. That list of the legal format codes in strftime python is mentioned below.


The DirectiveThe DescriptionThe Example
%aWeekday, short versionWed
%AWeekday, full versionWednesday
%wWeekday as a number 0-6, 0 is Sunday3
%dDay of the month 01-3131
%bMonth name, short versionDec
%BMonth name, full versionDecember
%mMonth as a number 01-1212
%yYear, short version, without the century18
%YYear, full version2018
%HHour 00-2317
%IHour 00-1205
%MMinute 00-5941
%SSecond 00-5908
%fMicrosecond 000000-999999548513
%zUTC offset+0100
%jDay number of year 001-366365
%UWeek number of year, Sunday as the first day of the week, 00-5352
%WWeek number of year, Monday as the first day of the week, 00-5352
%cLocal version of time and dateMon Dec 31 17:41:00 2018
%xLocal version of date12/31/18
%XLocal version of time17:41:00
%%A % character%

With this, we finish the python datetime now part of our python tutorial.

Latest Trending Technologies