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

Python time operation

編輯:Python
Get the current time
import datetime
import time
i = datetime.datetime.now()
print (" The current date and time is %s" % i)
print (" The current year is %s" %i.year)
print (" The current month is %s" %i.month)
print (" The current date is %s" %i.day)
print (" The current hour is %s" %i.hour)
print (" The current minute is %s" %i.minute)
print (" The current second is %s" %i.second)
Compare the time
i = str(datetime.datetime.now())
print(i[:10]) ###2022-06-17
s1=i[:10]
s2='2022-06-26'
s1_1 = time.strptime(s1, '%Y-%m-%d')
s2_2 = time.strptime(s2, '%Y-%m-%d')
print(s1_1>s2_2) ###False
Turn a time string into a timestamp
import time
s2='2022-06-23'
### Convert to time tuple first
s2_2 = time.strptime(s2, '%Y-%m-%d')
### And then turn to timestamp
res=int(time.mktime(s1_1))
## One more day
res=(datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
print(res)
## One more hour
res=(datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
print(res)
## One more minute
res=(datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")
print(res)

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