Home >>Python Tutorial >Python Datetime

Python Datetime

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
import datetime

 

x = datetime.datetime.now ( )

print ( x )
 

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.  
import datetime

 

x = datetime.datetime.now ( )

 

print ( x.year )

print ( x.strftime (“%A” ) )
 

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  
import datetime

 

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

 

print ( x )
  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  
import datetime

 

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

 

print ( x.strftime ( “%B” ) )
  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 Directive The Description The 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 the 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 the century 18
%Y Year, full version 2018
%H Hour 00-23 17
%I Hour 00-12 05
%p AM/ PM PM
%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 the week, 00-53 52
%W Week number of year, Monday as the first day of the week, 00-53 52
%c Local version of time and date 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 %
With this, we finish the python datetime now part of our python tutorial.

No Sidebar ads