程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python dry | time module and datetime module to print the special usage of time

編輯:Python

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved