Python Date and Time

There are various ways Python supplies date and time feature to add to the program. Python's Time and calendar module help in tracking date and time. Also, the 'DateTime' provide classes for controlling date and time by both simple and complex ways.

There are two different date and time objects in Python. These are:
  • naive
  • aware

The differences between 'naive' and 'aware' objects are:

  1. The 'aware' object holds the details about time-zone and daylight saving information whereas naïve objects do not have that feature.
  2. Naive objects are easy to understand and to work with.

Defining Tick

As we all can make an idea that time intervals have to be represented in floating point number. The floating-point numbers in units of seconds are signified by Tick in python. Particular instants of time are represented in seconds since 12:00 am, 1st of January, of the year 1990. A popular module name 'time' is available which provides functions that let's programmer work with time and made conversion between time-representation possible.

The pre-defined function time.time() is used to return the current time of the system.

The daytime module exports the constants listed below:

  • MINYEAR
  • MAXYEAR

Two programs are shown showing the use of two different modules:

Example:
import time;

ticktock = time.time();

print("Number of ticks:", ticktock)
Output:
Number of ticks: 1465927104.951356

Another program showing datetime module:

Example:
from datetime import datetime

presentime = datetime.now()

print(" NOW THE TIME IS:", presentime)

print("Todays Date is:", presentime.strftime('%Y-%m-%d') )

print("Year is:", presentime.year)

print("MOnth is:", presentime.month)

print("Day is:", presentime.day)
Output:
NOW THE TIME IS: 2016-06-14 18:01:25.831269
Todays Date is: 2016-06-14
Year is: 2016
MOnth is: 6
Day is: 14

Timetuple In Python

Python usually handles time's function as a tuple with nine numbers starting from zero (0) to eight (8).

0Four digit number (year)2016
1Months1 - 12
2Day1 - 31
3Hour0 - 23
4Minute0 - 59
5Second0 - 60
6Days of the week0 - 6 (where Zero designates Monday)
7Days of a year1 - 366
8Daylight saving-1, 0, 1, -1

Timedelta Object

It is used to represent duration, i.e.  the difference between two dates and or times.  For invoking this, the syntax is:

Syntax:
datetime.timedelta([days, [,seconds [,microseconds [,milliseconds [, minutes [,hours [,weeks]]]]]])

In the above scenario, all arguments are optional and their default value is zero (0).

Program To Get Current Time

Example:
import time;

curtime = time.localtime(time.time())

print("Current Time is:", curtime)
Output:
Current Time is: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=4, tm_sec=49, tm_wday=1, tm_yday=166, tm_isdst=0)

Calendar Of A Month

This facility is provided by "calendar" module which provides a wide range of methods to deal with monthly calendars. A simple program showing how to use:

Example:
import calendar

calndr = calendar.month(2016, 6)

print("The calendar for the month June of Year 2016 is:")

print (calndr)
Output:
The calendar for the month June of Year 2016 is:
     June 2016
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

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.