python中datetime模塊的使用


python中datetime模塊的使用方法

導入包 import datetime

獲取當天日期 now_time=datetime.datetime.now()

print((now_time+datetime.timedelta(days=+1)).strftime("%Y-%m-%d %H:%M:%S")) #獲取后一天+1  前一天-1

print ((now_time+datetime.timedelta(hours=-1)).strftime("%Y-%m-%d %H:%M:%S")) #獲取前一小時

print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1)).strftime("%Y-%m-%d %H:%M:%S"))   #前一分鍾后一分鍾

print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1/60)).strftime("%Y-%m-%d %H:%M:%S"))#前一秒后一秒
# 導入包
import calendar
import datetime

#獲取一個月或是N個月,一年中的天數
years = [2019,]
months = range(1, 13)

for year in years:
    for month in months:
        days = calendar.monthrange(int(year), int(month))[1]
        for day in range(1,(days)+1):
            format_datetime = datetime.datetime(year, month, day).strftime('%Y-%m-%d')
            print(format_datetime)
import calendar
import datetime


years = [2019,]
months = range(1, 13)

for year in settings.years:
   for month in settings.months:
       day = calendar.monthrange(int(year), int(month))[1]
       fromdate = datetime.date(year, month, 1)  # 開始時間: 2019-01-01
       todate = fromdate + datetime.timedelta(days=day + 1)  # 結束時間: 2019-02-02


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM