Google News
logo
Python Date and Time
Python is very useful in case of Date and Time. We can easily retrieve current date and time using Python. In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. 

The datetime classes in Python are categorized into main 5 classes.

date : Manipulate just date ( Month, day, year)
time :Time independent of the day (Hour, minute, second, microsecond)
datetime : Combination of time and date (Month, day, year, hour, second, microsecond)
timedelta : A duration of time used for manipulating dates
tzinfo :  An abstract class for dealing with time zones

Time intervals are floating-point numbers in units of seconds. To retrieve current time a predefined function localtime() is used. localtime() receives a parameter time.time() . Here, time is a module, The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970.
Example :
>>> import time;
>>> localtime = time.localtime(time.time())
>>> print ("Current Time is :", localtime)
Current Time is : time.struct_time(tm_year=2017, tm_mon=8, tm_mday=14, tm_hour=17, tm_min=43, tm_sec=54, tm_wday=0, tm_yday=226, tm_isdst=0)
>>> 

The time returned is a time structure which includes 9 attributes. These are summoned in the table given below.

AttributeDescription
tm_yearReturns the current year (2017).
tm_monReturns the current month (8).
tm_mdayReturns the current month day (14).
tm_hourReturns the current hour (17).
tm_minReturns the current minute (43).
tm_secReturns current seconds (54).
tm_wdayReturns the week day (0).
tm_ydayReturns the year day (226).
tm_isdstIt returns -1,0 or 1.

Time Format :

Python also support formatted time. Proceed as follows :

1. Pass the time structure in a predefined function asctime(). It is a function defined in time module.

2. It returns a formatted time which includes Day ,month, date, time and year.

3. Print the formatted time.

Example :
>>> import time;
>>> localtime = time.asctime( time.localtime(time.time()) )
>>> print ("Time Format :", localtime)
Time Format : Mon Aug 14 18:02:00 2017
>>> 

Python Time Modules

There is a popular time module available in Python which provides functions for working with times and for converting between representations. Here is the list of all available methods

Methods Description
time() Returns floating point value in seconds since epoch i.e.,12:00am, January 1, 1970
asctime(time) It takes the tuple returned by localtime() as parameter. It returns a 24 character string.
sleep(time) The execution will be stopped for the given interval of time.
strptime(String,format) It returns an tuple with 9 time attributes. It receives an String of date and a format.
gtime()/gtime(sec) It returns struct_time which contains 9 time attributes. In case seconds are not specified it takes current second from epoch.
mktime() Returns second in floating point since epoch.
strftime(format)/strftime(format,time) Returns time in particular format. If time is not given, current time in seconds is fetched.
tzset() Resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.
localtime([secs]) Accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the local time
clock( ) Returns the current CPU time as a floating-point number of seconds.