Date and time in Python - ProgrammerTech
Language:

Search

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
>>>time.time()
1583307387.367132

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
>>>time.ctime()
'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:

time.time(time.ctime())

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.

>>>time.ctime(time.time())
'Wed Mar  4 10:57:26 2020'
>>>time.ctime(1583307387.367132)
'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")
'Wed'

2- Show the whole word today.

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

3- Show the month in brief.

>>>time.strftime("%b")
'Mar'

4- Show the month a complete word.

>>>time.strftime("%B")
'March'

5- Display the full day, hour and date.

>>>time.strftime("%c")
'Wed Mar 4 10:57:26 2021'

6- Display the day as a number.

>>>time.strftime("%d")
'04'

7- Display the current hour without 24 minutes timezone.

>>>time.strftime("%H")
'18'

8- Display the current hour without 12 minutes timer.

>>>time.strftime("%H")
'10'

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

>>>time.strftime("%j")
'064'

10- Display the month as a number.

>>>time.strftime("%m")
'03'

11- Display the current minutes.

>>>time.strftime("%M")
'27'

12- Display the morning or evening time.

>>>time.strftime("%p")
'AM'

13- Displays the current seconds.

>>>time.strftime("%S")
'46'

14- Display the week number of the year.

>>>time.strftime("%U")
'09'

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

>>>time.strftime("%w")
'03'

16- Display the current date.

>>>time.strftime("%x")
'03/04/20'

17- Show the current time strftime.

>>>time.strftime("%X")
'10:57:26'

18- Presentation of the year in brief.

>>>time.strftime("%y")
'20'strftime

19- Display the year naturally.

>>>time.strftime("%Y")
'2020'

20- Display the time difference according to world time.

>>>time.strftime("%pZ")
'+03'

21- Display the full date and time.

>>> time.strftime("%d:%m:%Y:%H:%M:%S")
'04:03:2020:10:58:42'

 

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")
#result
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
0
1
2
3
4
5
6
7
8
9

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 https://youtu.be/_ovg6IAjxwU.


Muhammad Alush

Muhammad Alush

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

موقعي programmer-tech.com