All Courses

#### Master's In Artificial Intelligence Job Guarantee Program

4.5 (1,292 Ratings)

559 Learners

#### Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Jun 1st (5:32 PM) 515 Registered
More webinars

# Dealing With Date And Time in Python

Shashank Shanu

a year ago

• What is Tick?
• What is Time Tuple?
• How to get the Current Time in python?
• How to get the formatted time?
• How to get a calendar for a month?
• What is the calendar Module in Python?
• Some of the other Modules and Functions

Python program is capable of handling date and time in different ways. Python consists of a module named DateTime which is especially for handling different types of date and time format.

## What is Tick?

Time intervals are represented in units of seconds which will be the floating points numbers. A particular instant of time is expressed in seconds since 12:00 am, January 1, 1970(epoch).
Example
``````#!/usr/bin/python3
import time;      # importing time module.

num_ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", num_ticks)
``````
The output of the above code is:
``Number of ticks since 12:00am, January 1, 1970: 1594014539.0613124``

With ticks it’s easy to perform date arithmetic. But dates before the epochs cannot be represented in this form. We also cannot also represent dates in the far future in this way as the cutoff point for UNIX and Windows is sometime in 2038.

## What is TimeTuple?

Python provides many different time functions to handle time as a tuple of 9 numbers. Let’s see with an example.
Example −
``````import time

print (time.localtime())
``````
The output of the above code is:
``time.struct_time(tm_year=2020, tm_mon=7, tm_mday=6, tm_hour=11, tm_min=28, tm_sec=10, tm_wday=0, tm_yday=188, tm_isdst=0)``

The above output, a tuple is equivalent to struct_time structure. It consists of the below-mentioned attributes.

## How to get the Current Time in python?

We can translate a time instantly from seconds since the epoch floating-point value into a timetuple. We can do it by passing the floating-point value to a function (e.g., local time) that will return us a time-tuple with all the valid nine attributes.
``````#!/usr/bin/python3
import time

localtime = time.localtime(time.time())
print ("Local current time :", localtime)
``````
The output of the above code is mentioned below, which could be formatted in any another presentable form if we want−
``Local current time : time.struct_time(tm_year=2020, tm_mon=7, tm_mday=6, tm_hour=11, tm_min=35, tm_sec=2, tm_wday=0, tm_yday=188, tm_isdst=0)``

## How to get the formatted time?

Let’s suppose we want to format any time as per our requirements which is done with a simple method to get time in a readable format with asctime() -
``````#!/usr/bin/python3
import time

localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
``````
The output of the above code is:
``Local current time : Mon Jul  6 11:50:46 2020``

## How to get a calendar for a month?

Python has a calendar module which provides a wide range of methods to get yearly and monthly calendars.
Let’s take an example of the month of July 2020.
``````#!/usr/bin/python3
import calendar

cal = calendar.month(2020, 7)
print ("Here is the calendar:")
print (cal)
``````
The output of the above code is:
``````Here is the calendar:
July 2020
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 31
``````

## What is the Time Module in Python?

Python comes with a time module which is provides functions to work with times and for converting different times.
List of all the methods available are mentioned below-
The two most important attributes are available with a time module. Which are mentioned below −

## What is the calendar Module in Python?

Calendar module in python provides different calendar related functions which include a function to print a text calendar for a given month or year.
By default, the calendar module takes Monday as the first day of the week and Sunday as the last one. If you want to change this, we can call the calendar.setfirstweekday() function.
List of functions available with the calendar module −

## Some of the other Modules and Functions

If you are interested, then here you would find a list of other important modules and functions to play with date & time in Python −