Python Date and Time

Python Date & Time is one more basic thing in python programming. In Python Date and Time can be handled in several ways. One common chore for computers is converting between date formats. Python’s time and calendar modules help track dates and times.

Defining Tick:

In Python, Tick is a statistical learning library. Which particular emphasis on time-dependent models, such as point processes, and tools for generalized linear models and survival analysis.Python Tick signifies the floating-point numbers in units of seconds in Python.

There is another popular module name Time is available in python, which provides those functions which let programmers work with the time and made conversion between time-representation possible. For this a pre-defined function is used named time.time() to return the present time of the system.

#tick in python

import time;
ticks = time.time();
print("The number of ticks:", ticks)

TimeTuple in Python:

Some Python’s time functions handle time as a tuple of 9 numbers. The attribute is equivalent to struct_time structure. As given below in the table,

04 Digit Yeartm-year2021
6Days of the Yeartm-yday1-366
7Days of the Weektm-wday0-6
8Daylight Savingtm-isdst-1, 0 , 1, -1

Get Current Time in Python:

To translate a time instantly from a second floating-point value into a time-tuple, we simply have to pass the floating-point value to a function named Local time, which returns a time-tuple with all nine items valid described in the above table.

#getting current time
import time;

Current Time = time.localtime(time.time())
print "Current Time is :", Current Time

Get Formatted Time in Python:

To make the output readable and easy to understand we have to put the time in the proper format. For this purpose asctime() function is used. It makes the time readable.

import time;

localtime = time.asctime( time.localtime(time.time()) )
print "Current time is :", localtime

Get Calendar for a Month:

There is a wide range of methods in Calendar Module which use to play with yearly and monthly calendars.Given below is the print of calendar.

import calendar

Calendar = calendar.month(2021, 2)
print "The calendar of this month:"
print calendar

The time Modules:

Another popular module in Python is the time module which provides functions for working with times and for converting between representations. Given below are the details of all available methods in the time module, with examples.

1) time.clock():

time.clock() module is used to return the current processor time as a floating-point number expressed in seconds on Unix.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating-point number.

#importing time module 
import time 
#Get the current processor 
#time in seconds 
now_time = time.clock() 
print("Current processor time in seconds is :", now_time) 

2) time.ctime():

Python time.ctime()  is used to convert a time expressed in seconds to a string representing local time. If seconds are not provided or None, the current time as returned by time() is used. This function is equivalent to asctime(localtime(secs)). ctime() can’t use locale information.


import time

seconds = 2838337.837487584
local_time = time.ctime(seconds)
print("Local time:", local_time)

3) time.altzone():

Another Python time module attribute is time.altzone(). This is used to returns the offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC. Only use this if daylight is nonzero.

#time.altzone program

import time
print "time.altzone %d " % time.altzone

4) time.sleep():

The time.sleep() function suspends/delays the execution of the current thread for the given number of seconds in the program.

#time.sleep() program
import time

print "Time Start : %s" % time.ctime()
time.sleep( 2.5 )
print "Time End : %s" % time.ctime()

5) time.localtime():

The time.localtime() function takes the number of seconds passed an argument and returns struct.time in local time.


import time

print "time.localtime() : %s" % time.localtime()

6) time.gmtime():

Accepts an instant expressed in seconds and returns a time-tuple t with the UTC time. In this t.tm_isdst is always 0.

import time

result = time.gmtime(183455543)
print("result:", result)
print("tm_hour:", result.tm_hour)
print("\nyear:", result.tm_year)

7) time.time():

Returns the current time instant, a floating-point number of seconds since the epoch.


import time
seconds = time.time()
print("Seconds since epoch =", seconds)	

8) time.strftime(fmt[,t]):

time.strftime(fmt[,t]) accepts an instant expressed as a time-tuple in local time and returns a string representing the instant as specified by string fmt.

#time.strftime(fmt[,t]) function 

from time import gmtime, strftime 
#using format of showing time 
t1 = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime()) 
print("Example 1:", t1) 
#only change in this is the full names 
#and the representation 
t2 = strftime("%A, %D %B %Y %H:%M:%S + 2020", gmtime()) 
print("Example 2:", t2) 

9) time.mktime():

time.mktime() function is used to convert a time.struct_time object or a tuple containing 9 elements corresponding to time.struct_time object to time in seconds passed since epoch in local time.

#time.mktime() function 

import time

t = (2021, 5, 10, 12, 5, 25, 2, 40, 0)
secs = time.mktime( t )
print "time.mktime(t) seconds : %f" %  secs
print "asctime(localtime(secs)): %s" % time.asctime(time.localtime(secs))

10) time.tzset():

time.tzset() resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.


#time.tzset() function

import time
import os

os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
print "Time =", time.strftime('%X %x %Z')


Following are two important attributes available with time module in python .

time.timezoneAttribute time.timezone is the offset in seconds of the local time
zone (without DST) from UTC (>0 in the Americas;
<=0 in most of Europe, Asia, Africa).
time.tznameAttribute time.tzname is a pair of locale-dependent
strings, which are the names of the local time
zone without and with DST, respectively.