Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - DATE & TIME


Date and Time is one of the important topic in any programming language. In Python we can import the module named 'datetime' to work with Date and Time.


Let us start with the below example.


Example :



import datetime

x = datetime.datetime.now()
print(x)


Output :



  2020-12-10 18:59:37.476059

Now, if we dissect the output,


java_Collections

So, with the below statement,


x = datetime.datetime.now( )

We get a 'datetime' object, 'x' that has Year, Month, Day, Hour, Minute, Second and Microsecond.


So, you got the date in clubbed format. Now, what if you want everything separately.


Well! Python provides that way as well.


Example :



import datetime

x = datetime.datetime.now()
year = x.year
month = x.month
day = x.day
hour = x.hour
minute = x.minute
second = x.second
microsecond = x.microsecond
    
print("The full format is ", x)
print("The year is ",year)
print("The month is ",month)
print("The day is ",day)
print("The hour is ",hour)
print("The minute is ",minute)
print("The second is ",second)
print("The microsecond is ",microsecond)


Output :



  The full format is  2020-12-10 19:18:18.228909
  The year is 2020
  The month is 12
  The day is 10
  The hour is 19
  The minute is 18
  The second is 18
  The microsecond is 228909

Now, if you look at the above output, we have separated the clubbed formatted date.


To display the year, you need to invoke 'x.year'.


year = x.year

Similar, logic applies for Month, Day, Hour, Minute, Second and Microsecond.


Now, let us see how to work with Date and Time separately.


Working with Dates


To work only with Dates. We can only import 'date' class from the 'datetime' module.


Let us look at the below example.


Example :



from datetime import date
today = date.today()
print("Today's date is ",today)    


Output :



  Today's date is  2020-12-10

So, in the above example, we have just imported the 'date' class from the 'datetime' module.


from datetime import date

Next, we have invoked the Function 'today( )' of 'date' class to get today's date.


today = date.today( )

And if you see the output.


Today's date is  2020-12-10

Only the date is displayed and not the time.


Next, let us see, how to work with time.


Working with Time


Similarly, to work only with Time. We can only import the 'time' module.


Example :



import time

x = time.localtime()
t = time.strftime("%H:%M:%S", x)
print(t)  


Output :



  20:09:46

So, in the above example, we have used the 'localtime( )' Function from the 'time' module and got the unformatted time in the variable 'x'.


x = time.localtime( )

Then we have used 'strftime( )' Function to format the String.


t = time.strftime("%H:%M:%S", x)

- Where '%H' stands for Hour,


-'%M' stands for Minute,


- And '%S' stands for Second.


And we get the time in Hour, Minute and Seconds.


20:09:46

'strftime( )' Function itself needs an explanation. Let us look at it next.


'strftime( )' Function


To work with Date and Time and to format it properly. There is a Function called 'strftime( )'.


Apart from the 'time' module, it is also present in the 'datetime' module.


In 'datetime' module it takes just one argument and you can format the date and time in the way you want to.


Let us look at it in the below example.


Example :



import datetime

x = datetime.datetime.now()
t = x.strftime("%d-%B-%Y")
print(t)


Output :



  10-January-2021

As we have seen earlier, with the below statement.


x = datetime.datetime.now( )

We get a 'datetime' object, 'x' that has Year, Month, Day, Hour, Minute, Second and Microsecond.


Then we have used the 'strftime( )' function to display the date in a proper format.


t = x.strftime("%d-%B-%Y")

Where the formats,


-'%d' is to display the day.


-'%B' is to display the Month


-'%Y' is to display the Year.


And we get the date in the below format,


10-January-2021

Now let us see all the formats available for 'strftime( )' function.



Directive Meaning Represented as
%y Short Version for Year 21
%Y Full Version for Year 2021
%H Hour 00-23
%I Hour 00-12
%p AM/PM AM/PM
%M Minute 00-59
%j Day number in a year 001-366
%U Week number in a year. Sunday is the first day of week. 00-53
%W Week number of year. Monday is the first day of week. 00-53
%c Date and Time Sun Jan 10 20:38:00 2021
%x Date 01/10/21
%X Time 20:38:00
%S Second 00-59
%f Microsecond 000000-999999
%z UTC offset +0100
%a Short form of Weekday Sun
%A Full form of Weekday Sunday
%w Weekday in number 0-6, 0 is Sunday
%d Day of month 01-31
%b Name of the Month in Short Jan
%B Name of the Month in Full January
%m Month in the form of number 01-12
%Z Timezone CST