Date and Time

Python is very useful in case of Date and Time. We can easily retrieve current date and time using Python.

Retrieve Time

To retrieve current time a predefined function localtime() is used. localtime() receives a parameter time.time() . Here,

time is a module,

time() is a function that returns the current system time in number of ticks since 12:00 am , January 1,1970. It is known as epoch.

Tick is simply a floating point number in seconds since epoch.

eg:

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

Output:

>>>
Current Time is :time.struct_time(tm_year=2014, tm_mon=6, tm_mday=18, tm_hour=12,  
tm_min=35, tm_sec=44, tm_wday=2, tm_yday=169, tm_isdst=0)
>>>

Calendar

Python provides calendar module to display Calendar.

Eg:

import calendar
print "Current month is:"
cal = calendar.month(2014, 6)
printcal

Output:

>>>
Current month is:
      June 2014
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
>>>