Working With Date And Time In Python

Introduction

In this article, we will discuss various examples of 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 allows us to get the current date and time. The import statement gives us access to the functionality of datetime class.

Getting Current Date in Python

#importing datetime class  
import datetime  
  
#getting today's date and saving in CurrentDate Variable  
CurrentDate=datetime.date.today()  
  
#prining current date from CurrentDate  
print(CurrentDate) 

Output

Current Date in Python

The default format of the 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

import datetime  
CurrentDate=datetime.date.today()  
#Displaying Current Day  
print(CurrentDate.day)  
  
#Displaying Current Month  
print(CurrentDate.month)  
  
#Displaying Current Year  
print(CurrentDate.year)  

Output

Displaying Current Day, Month And Year Separately

Format Date and Time in Python

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

Example

import datetime  
CurrentDate=datetime.date.today()  
#%d is for date  
#%b is for month  
#Y is for Year  
print(CurrentDate.strftime('%d-%b-%Y'))  

Output

Format Date and Time in Python

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).

Code Meaning Example
%a Weekday as locale’s abbreviated name. Tue
%A Weekday as locale’s full name. Tuesday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 2
%d Day of the month as a zero-padded decimal number. 03
%-d Day of the month as a decimal number. (Platform specific) 3
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. september
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y A year without century as a zero-padded decimal number. 13
%Y A year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 09
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 9
%I Hour (12-hour clock) as a zero-padded decimal number. 09
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 9
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the object is naive).  
%Z Time zone name (empty string if the object is naive).  
%j Day of the year as a zero-padded decimal number. 246
%-j Day of the year as a decimal number. (Platform specific) 246
%U Week 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
%W Week 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
%c Locale’s appropriate date and time representation. Tue Sep 3 09:06:05 2013
%x Locale’s appropriate date representation. 09/03/13
%X Locale’s appropriate time representation. 09:06:05
%% A literal '%' character. %

You can also write a complete Message inside the strftime function.

Example

import datetime  
CurrentDate=datetime.date.today()  
  
print(CurrentDate.strftime('Join My Birthday party on %A, %d-%B-%Y'))  

Output

Date and Time in Python

Convert String into Date in Python

Suppose you are taking input from the user on the console, then whatever the user types in console screen that will be a string. Suppose you are asking about your B'Day Date, then that will be a string and you have to explicitly convert that string into date format. 

import datetime  
birthday=input("What is your B'day? ")  
print("Your B'day is :"+birthday)  

Output 

Convert String into Date in Python

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

Convert String into Date in Python

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

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

Output 

Convert String into Date in Python

Date and Time Calculations in Python

Example: How many days are remaining for your next birthday? 

import datetime  
birthday=input("What is your next B'day Date? (in DD/MM/YYYY) ")  
birthdate=datetime.datetime.strptime(birthday,"%d/%m/%Y").date()  
today=datetime.date.today();  
days=birthdate-today;  
print(days.days);  

Output

Date and Time Calculations in Python


Recommended Free Ebook
Similar Articles