Foreword
Python has many third-party modules that provide us with very convenient help in our work. Taking advantage of these modules can even improve our work efficiency faster.
This article is about two old friends we often use - time and datetime, without further ado, let's go directly to today's theme dinner with the editor.
time module
1. When we want to print today's date and time, we can use the following line of code
import timenow_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())print(now_time)
The results are as follows:
2022-08-02 19:14:16
2. Only print today's date, you can use the following line of code
import timenow_time = time.strftime('%Y-%m-%d', time.localtime())print(now_time)
The results are as follows:
2022-08-02
datetime module
3. When you want to print today's date, not only time, you can also use datetime to solve
import datetimetoday = datetime.date.today()print(today)
The results are as follows:
2022-08-02
4. To print yesterday's date, you can use the following code
import datetimetoday = datetime.date.today()yesterday = today - datetime.timedelta(days=1)print(yesterday)
The results are as follows:
2022-08-01
5. To print tomorrow's date, you can use the code below
import datetimetoday = datetime.date.today()tomorrow = today + datetime.timedelta(days=1)print(tomorrow)
The results are as follows:
2022-08-03
6. When you want to print two months from today, you can use the following code
import datetimetoday = datetime.date.today()mb_day = today - datetime.timedelta(days=60)print(mb_day)
The results are as follows:
2022-06-03
Remember to bookmark and follow the editor, the python dry goods in the back are waiting for you.
If you like this article or this article is helpful to you, remember to follow the editor and like it. If you have any questions or needs, please leave a private message.
How does Mysql query two data tables at the same time for the same fields in two tables