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

Python article series - timestamp details

編輯:Python
  • datetime&time
#!/usr/bin/python3
# Time stamp
import time
print (" The current timestamp is :", time.time())
# Interval between function executions
import time
def calculateTime():
item = 1
for i in range(1,100000):
item = item + i
return item
startTime = time.time()
result = calculateTime()
endTime = time.time()
print(' The result of the calculation is :'+ str(result))
print(' execution time :'+ str(endTime - startTime))
# Pause time
import time
for i in range(2):
print('one')
print(time.time())
time.sleep(1)
print('two')
print(time.time())
time.sleep(1)
print(' Run complete ')
# Current date and time
import datetime
print(datetime.datetime.now())
# Get the specified time
datetest = datetime.datetime(2019,9,30,22,22,0)
print(datetest)
print(str(datetest.year)+"-"+str(datetest.month)+"-"+str(datetest.day)+" "+str(datetest.hour)+":"+str(datetest.minute)+":"+str(datetest.second))
# Convert timestamps to datetime object
dt1 = datetime.datetime.fromtimestamp(10000)
dt2 = datetime.datetime.fromtimestamp(time.time())
print(dt1)
print(dt2)
# Date conversion
datestr = datetime.datetime.strptime('2019-9-30 22:10:00', '%Y-%m-%d %H:%M:%S')
print(datestr)
now = datetime.datetime.now()
print(now.strftime('%a, %b %d %H:%M'))
# Date increase and decrease
now = datetime.datetime.now()
print(now)
newdate = now + datetime.timedelta(hours=10)
print(newdate)
newdate = now - datetime.timedelta(days=1)
print(newdate)

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