Date and time in Python - ProgrammerTech


Date and time in Python

  • Share this:
Date and time in Python

Date And Time

Date and time In Python, there are many modules that help us display the date and time, and as it is clear from the name of the lesson that we are dealing with date and time, it is important if we are working on code or an application and want to display the date And time it.

>>>import time

We note that the date and time are printed in seconds, and to display it in the normal way we write as follows:

>>>import time
'Wed Mar  4 10:38:12 2020'

We also noticed that it printed the date and time for us in a regular manner according to the time on my computer, but if we wrote as follows:


It will give us the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: time() takes no arguments (1 given)

This means that it needs 1 argoment from us, and we cannot enter time.time and enter an argument like ctime because it is wrong because ctime takes seconds and the assumption is written in the following form to show us the date and time.

'Wed Mar  4 10:57:26 2020'
'Wed Mar  4 10:57:26 2020'


strftime method in Python

This method creates a string that includes the current date and deals with it, with multiple directives used. We will explain several examples that deal with strftime.

1- Presentation of the day in a nutshell.

>>> time.strftime("%a")

2- Show the whole word today.

>>> time.strftime("%A")

3- Show the month in brief.


4- Show the month a complete word.


5- Display the full day, hour and date.

'Wed Mar 4 10:57:26 2021'

6- Display the day as a number.


7- Display the current hour without 24 minutes timezone.


8- Display the current hour without 12 minutes timer.


9- Displays the current day number of the year from 001 to 365.


10- Display the month as a number.


11- Display the current minutes.


12- Display the morning or evening time.


13- Displays the current seconds.


14- Display the week number of the year.


15- Display the number of the day of the week.


16- Display the current date.


17- Show the current time strftime.


18- Presentation of the year in brief.


19- Display the year naturally.


20- Display the time difference according to world time.


21- Display the full date and time.

>>> time.strftime("%d:%m:%Y:%H:%M:%S")


strptime method in Python

This method creates an object from a string that represents the date and time with special formatting tags.

Example 1

>>>import time
>>> time.strptime("04 03 2020","%d %m %Y")
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=64, tm_isdst=-1)
date and time python


The meaning of the question we gave him a day, a month and a year and after it we gave him symbols of their significance to distinguish them for us in the answer and as you noticed the situation before the year tm_year and before the month and before the day and the hour he left it zeros 0 because we did not ask him to do so.

Example 2

>>>import time
>>> time.strptime("04 10 2021 12 30 5","%d %m %Y %H %M %S")
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=4, tm_hour=12, tm_min=30, tm_sec=5, tm_wday=0, tm_yday=277, tm_isdst=-1)
date and time python time.time


Using sleep over time in Python

The sleep() function suspends code (waiting code) for a specified number of seconds and is used in the time library. To understand the meaning of sleep more clearly, we will take the following example using the sleep library.

import time
def sleepwithloop () :
    for i in range (10) :
        time.sleep (2)
        print (i)
#Run the code by calling the function by its name
sleepwithloop()    #function call to run

It will print numbers from 0 to 9 for us, but between printing each number it waits two seconds and prints the next number and stops at the number 9.

Watch the lesson on YouTube from the link

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.