Python Date

In the real world applications, there are the scenarios where we need to work with the date and time. There are the examples in python where we have to schedule the script to run at some particular timings.

In python, the date is not a data type, but we can work with the date objects by importing the module named with datetime, time, and calendar.

In this section of the tutorial, we will discuss how to work with the date and time objects in python.


Tick

In python, the time instants are counted since 12 AM, 1st January 1970. The function time() of the module time returns the total number of ticks spent since 12 AM, 1st January 1970. A tick can be seen as the smallest unit to measure the time.

Consider the following example.

Example

  1. import time;  
  2.   
  3. #prints the number of ticks spent since 12 AM, 1st January 1970  
  4.   
  5. print(time.time())  

Output:

1545124460.9151757

How to get the current time?

The localtime() functions of the time module are used to get the current time tuple. Consider the following example.

Example

  1. import time;  
  2.   
  3. #returns a time tuple   
  4.   
  5. print(time.localtime(time.time()))  

Output:

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=18, tm_hour=15, tm_min=1, 
tm_sec=32, tm_wday=1, tm_yday=352, tm_isdst=0)

Time tuple

The time is treated as the tuple of 9 numbers. Let's look at the members of the time tuple.

Index Attribute Values
0 Year 4 digit (for example 2018)
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0 to 60
6 Day of weak 0 to 6
7 Day of year 1 to 366
8 Daylight savings -1, 0, 1 , or -1

Getting formatted time

The time can be formatted by using the asctime() function of time module. It returns the formatted time for the time tuple being passed.

Example

  1. import time;  
  2.   
  3. #returns the formatted time    
  4.   
  5. print(time.asctime(time.localtime(time.time())))  

Output:

Tue Dec 18 15:31:39 2018

Python sleep time

The sleep() method of time module is used to stop the execution of the script for a given amount of time. The output will be delayed for the number of seconds given as float.

Consider the following example.

Example

  1. import time  
  2. for i in range(0,5):  
  3.     print(i)  
  4.     #Each element will be printed after 1 second  
  5.     time.sleep(1)  

Output:

0
1
2
3
4

The datetime Module

The datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc.

To work with dates as date objects, we have to import datetime module into the python source code.

Consider the following example to get the datetime object representation for the current time.

Example

  1. import datetime;  
  2.   
  3. #returns the current datetime object   
  4.   
  5. print(datetime.datetime.now())  

Output:

2018-12-18 16:16:45.462778

Creating date objects

We can create the date objects by passing the desired date in the datetime constructor for which the date objects are to be created.

Consider the following example.

Example

  1. import datetime;  
  2.   
  3. #returns the datetime object for the specified date  
  4.   
  5. print(datetime.datetime(2018,12,10))  

Output:

2018-12-10 00:00:00

We can also specify the time along with the date to create the datetime object. Consider the following example.

Example

  1. import datetime;  
  2.   
  3. #returns the datetime object for the specified time    
  4.   
  5. print(datetime.datetime(2018,12,10,14,15,10))  

Output:

2018-12-10 14:15:10

Comparison of two dates

We can compare two dates by using the comparison operators like >, >=, <, and <=.

Consider the following example.

Example

  1. from datetime import datetime as dt  
  2. #Compares the time. If the time is in between 8AM and 4PM, then it prints working hours otherwise it prints fun hours  
  3. if dt(dt.now().year,dt.now().month,dt.now().day,8)<dt.now()<dt(dt.now().year,dt.now().month,dt.now().day,16):  
  4.     print("Working hours....")  
  5. else:  
  6.     print("fun hours")  

Output:

fun hours

The calendar module

Python provides a calendar object that contains various methods to work with the calendars.

Consider the following example to print the Calendar of the last month of 2018.

Example

  1. import calendar;  
  2. cal = calendar.month(2018,12)  
  3. #printing the calendar of December 2018  
  4. print(cal)  

Output:


python-date-and-time
python-date-and-time


Printing the calendar of whole year

The prcal() method of calendar module is used to print the calendar of the whole year. The year of which the calendar is to be printed must be passed into this method.

Example

  1. import calendar  
  2.   
  3. #printing the calendar of the year 2019  
  4. calendar.prcal(2019)  

Output:


Python Date and time
Python Date and time



;