Python Date & Time

Date & time in python

A Python program can handle Python Date & Time in several ways. Converting between date formats is a common chore for computers. Python’s time and calendar modules help track dates and times.


What is Tick?

Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).

There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).


Example:

#!/usr/bin/python
import time;  # This is required to include time module.

ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks

Output :

Number of ticks since 12:00am, January 1, 1970: 7186862.73399

Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way – the cutoff point is sometime in 2038 for UNIX and Windows.

What is Time Tuple?

Many of Python’s time functions handle time as a tuple of 9 numbers, as shown below −

IndexFieldValues
04-digit year2008
1Month1 to 12
2Day1 to 31
3Hour0 to 23
4Minute0 to 59
5Second0 to 61 (60 or 61 are leap-seconds)
6Day of Week0 to 6 (0 is Monday)
7Day of year1 to 366 (Julian day)
8Daylight savings-1, 0, 1, -1 means library determines DST

The above tuple is equivalent to struct_time structure. This structure has following attributes −

IndexAttributesValues
0tm_year2008
1tm_mon1 to 12
2tm_mday1 to 31
3tm_hour0 to 23
4tm_min0 to 59
5tm_sec0 to 61 (60 or 61 are leap-seconds)
6tm_wday0 to 6 (0 is Monday)
7tm_yday1 to 366 (Julian day)
8tm_isdst-1, 0, 1, -1 means library determines DST

Getting formatted time

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

Example:

import time    
  #returns the formatted time      
  
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 provided as the float.

Consider the following example.

Example:

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

Output:

0
1
2
3
4

The Python Date & Time 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 the datetime module into the python source code.

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

Example:

import datetime  
#returns the current datetime object     
print(datetime.datetime.now())    

Output:

2020-04-04 13:18:35.252578

Creating date objects

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

Consider the following example.

Example:

import datetime    
#returns the datetime object for the specified date    
print(datetime.datetime(2020,04,04))    

Output:

2020-04-04 00:00:00

Printing the calendar of whole year

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

Example:

import calendar    
#printing the calendar of the year 2019    
s = calendar.prcal(2020)  

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *