Python Date & Time

 

Date & Time

Dates and timings are very important for computers. Computers keeps tracks of date and time for files, documents and various other operations. 

Similarly python uses date and time to perform several operations.

 

Example:

import time
print(time.time())

Output:

1658659152.7263992

So, what can we understand from the about output? What does a bunch of random numbers mean?


These numbers are nothing but the number of ticks since the start of January 1, 1970 also called as epoch.


Now, one might ask what are ticks?


Ticks are floating point numbers measured in units of seconds for time interval.

 

We can also pass the epoch inside the ctime() function (or simply use the ctime() function) to print the current time in human readable format.


Example:

import time
print(time.ctime())

Output:

Sun Jul 24 16:24:50 2022

 

time.sleep():

the sleep() function inside time module is used to delay execution of current time thread by the given number of seconds.


Example:

import time

time.sleep(10)
print(time.ctime())


Output:

Sun Jul 24 16:24:50 2022

As we can see, the sleep() will delay the execution 10 seconds.

 

Time.struct_time_Class:

Index

Field

Attribute

Value

Meaning

Format

Codes

0

Year

tm_year

0000, ………., 9999

Four digit year

%Y

1

Month

tm_mon

1 – January, 2 – February, …….,

12 - December

Months in a year

%m

2

Day

tm_mday

1, 2, 3, ……., 29, 31

Days in a month

%d

3

Hour

tm_hour

0, 1, 2, ……., 22, 23

Hours in a day

%H

4

Minute

tm_min

0, 1, 2, ……., 58, 59

Minutes in an hour

%M

5

Second

tm_sec

0, 1, 2, ……., 60, 61

Seconds in a minute

%S

6

Day of Week

tm_wday

0 – Monday, 1 – Tuesday, ……., 6 – Sunday

Days in a week

%w

7

Day of Year

tm_yday

1, 2, 3, ……., 355, 356

Days in a year

%j

8

Daylight savings

tm_isdst

-1, 0, 1

 

 

 

time.localtime():

To get the output of local time in struct_time format, use the localtime() function.

 

Example:

import time

print(time.localtime(1658672956.8853111))

Output:

time.struct_time(tm_year=2022, tm_mon=7, tm_mday=24, tm_hour=19, tm_min=59, tm_sec=16, tm_wday=6, tm_yday=205, tm_isdst=0)

 

time.gmtime():

To get the output of Coordinated Universal Time in struct_time format, use the gmtime() function.


Example:

import time

print(time.gmtime(1658672956.8853111))

Output:

time.struct_time(tm_year=2022, tm_mon=7, tm_mday=24, tm_hour=14, tm_min=29, tm_sec=16, tm_wday=6, tm_yday=205, tm_isdst=0)

 

time.mktime():

We also have the reverse of localtime() function that prints seconds passed since epoch in local time. 


Example:

import time

local_time = (2022, 7, 24, 20, 14, 39, 6, 205, 0)
print(time.mktime(local_time))

Output:

1658673879.0

 

time.asctime():

The asctime() function takes struct_time and prints a single string representing it.

Example:

import time

local_time = (2022, 7, 24, 20, 14, 39, 6, 205, 0)
print(time.asctime(local_time))

Output:

Sun Jul 24 20:14:39 2022

 

time.strptime():

This function parses string based on given format and returns it in struct_time format.


Example:

import time

local_time = "24 July, 2022"
print(time.strptime(local_time, "%d %B, %Y"))


Output:

time.struct_time(tm_year=2018, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=205, tm_isdst=-1)

 

Calendar:

We can even print a calendar for a particular month. This can be done using the calendar module.


Example:

import calendar

print(calendar.month(2022, 7))

Output:

     July 2022
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