Linux

Python Date, Calendar, time

In program many time we need date and time, here we will learn some usable function related to time. The Python datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported,

Python datetime module

Here we'll find out today date by using Python datetime module. In order to print today's day, datetime.time.today() will be used as shown in figure below.
Python Datetime module
Python Datetime

The out-put is easy to see but very difficult to use in program. If you want to use the date string, you have to use isoformat() Consider we want to print today's date, use datetime.date.today().isoformat()
As show in figure below.
Python Datetime in real life using datetime.date.today().isoformat()
Datetime in real life

Now we'll print the current time using datetime.datetime.now().isoformat()
Python Datetime now in real life using datetime.datetime.now().isoformat()
Datetime now in real life

Consider a situation where you want to calculate the processing time of running program. Use datetime.datetime.now() as shown in figure given below.
time difference using Python datetime.datetime.now()
Showing time difference
Time difference is about 52 seconds.

Python Calendar module

Now we'll use calendar module to print the calendar of particular month. In order to print particular month, calendar.month(year, month) would be used as shown in figure below.
Python calendar.month(year, month)
Calendar
Output is very beautiful, you make calendar of any month.


Python time module

The Python time module gives you very interesting things.
To produce delay in the the program, use time.sleep(seconds) function.
The time.time() gives float time value.
The time.ctime() gives the human readable time.
If you want to print to time after certain seconds, use time.ctime((time.time() + 15)).
The below example shows all above things.
time.sleep(seconds),time.time(), time.ctime(),time.ctime((time.time() + 15)) using Python
TIme module
Click to view code





admin