Working With Date And Time In Python

Introduction

In this article we will discuss various examples about getting Date and Time in Python. There are several ways to handle date and time in Python. In this article we will discuss datetime class which allow us to get the current date and time. The import statement gives us to access to the functionality of datetime class.
 
Getting Current Date in Python
  1. #importing datetime class  
  2. import datetime  
  3.   
  4. #getting today's date and saving in CurrentDate Variable  
  5. CurrentDate=datetime.date.today()  
  6.   
  7. #prining current date from CurrentDate  
  8. print(CurrentDate)  
Output:

 

Default format of date is Year-Month-Date. But you can change your date format which we will see in further examples.

Displaying Current Day, Month And Year Separately:
  1. import datetime  
  2. CurrentDate=datetime.date.today()  
  3. #Displaying Current Day  
  4. print(CurrentDate.day)  
  5.   
  6. #Displaying Current Month  
  7. print(CurrentDate.month)  
  8.   
  9. #Displaying Current Year  
  10. print(CurrentDate.year)  
Output:

 

Format Date and Time

To format date and time in Python we use strftime function. strftime allows you to format date and time.

Example:
  1. import datetime  
  2. CurrentDate=datetime.date.today()  
  3. #%d is for date  
  4. #%b is for month  
  5. #Y is for Year  
  6. print(CurrentDate.strftime('%d-%b-%Y'))  
Output:



In the above example:
%d=>Is a code for Date
%b=>Is a code for Month
%Y=>Is a code for Year 

All the date formats code are listed in the following table: (Table Reference).

CodeMeaningExample
%aWeekday as locale’s abbreviated name.Tue
%AWeekday as locale’s full name.Tuesday
%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.2
%dDay of the month as a zero-padded decimal number.03
%-dDay of the month as a decimal number. (Platform specific)3
%bMonth as locale’s abbreviated name.Sep
%BMonth as locale’s full name.september
%mMonth as a zero-padded decimal number.09
%-mMonth as a decimal number. (Platform specific)9
%yYear without century as a zero-padded decimal number.13
%YYear with century as a decimal number.2013
%HHour (24-hour clock) as a zero-padded decimal number.09
%-HHour (24-hour clock) as a decimal number. (Platform specific)9
%IHour (12-hour clock) as a zero-padded decimal number.09
%-IHour (12-hour clock) as a decimal number. (Platform specific)9
%pLocale’s equivalent of either AM or PM.AM
%MMinute as a zero-padded decimal number.06
%-MMinute as a decimal number. (Platform specific)6
%SSecond as a zero-padded decimal number.05
%-SSecond as a decimal number. (Platform specific)5
%fMicrosecond as a decimal number, zero-padded on the left.000000
%zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). 
%ZTime zone name (empty string if the object is naive). 
%jDay of the year as a zero-padded decimal number.246
%-jDay of the year as a decimal number. (Platform specific)246
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.35
%WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.35
%cLocale’s appropriate date and time representation.Tue Sep 3 09:06:05 2013
%xLocale’s appropriate date representation.09/03/13
%XLocale’s appropriate time representation.09:06:05
%%A literal '%' character.%

You can also write complete Message inside the strftime function.

Example:
  1. import datetime  
  2. CurrentDate=datetime.date.today()  
  3.   
  4. print(CurrentDate.strftime('Join My Birthday party on %A, %d-%B-%Y'))  
Output:

 

Convert String into Date

Suppose you are taking input from the user on console, then whatever user types in console screen that will be string. Suppose you are asking about your B'Day Date, then that will be string and you have to explicitly convert that string into date format. 
  1. import datetime  
  2. birthday=input("What is your B'day? ")  
  3. print("Your B'day is :"+birthday)  

Output: 

 

But in the above example whatever user inputs is string. Now user can also enter his/her birthday date like the following:

 

Now you can see above that with my date of birth I am also entering some string that is not fine because I can also do some calculations with my date of birth. So if I have to convert it into date then only some calculations are applied.

To convert string to date format we have strptime function.

Example: 
  1. import datetime  
  2. birthday=input("What is your B'day? (in DD/MM/YYYY) ")  
  3. birthdate=datetime.datetime.strptime(birthday,"%d/%m/%Y").date()  
  4. print("Your B'day is : "+birthdate.strftime('%d/%B/%Y'))  
Output: 

 

Calculations on Date and Time

Example: How many days are remaining for you next birthday? 
  1. import datetime  
  2. birthday=input("What is your next B'day Date? (in DD/MM/YYYY) ")  
  3. birthdate=datetime.datetime.strptime(birthday,"%d/%m/%Y").date()  
  4. today=datetime.date.today();  
  5. days=birthdate-today;  
  6. print(days.days);  
 Output:

 

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com