一、獲取當前時間
import datetime
# 2019-7-9
print
(datetime.datetime.now(
).year)
# 2019
print
(datetime.datetime.now(
).month)
# 7print
(datetime.datetime.now(
).day)
# 9#上面的結果是正數型
#下面獲取字串型
print
(datetime.datetime.now(
).strftime(
'%m'))
# '07'
print
(datetime.datetime.now(
).strftime(
'%d'))
# '09'
print
(datetime.datetime.now(
).strftime(
'%y'))
# '19'
print
(datetime.datetime.now(
).strftime(
'%y'))
# '2019'
二、輸入乙個整數,將其變成日期型別
'''
'''import time
defsecs2datestr
(microsecs):if
int(microsecs)
<0:
return
""return
str(time.strftime(
"%y-%m-%d %h:%m:%s"
, time.localtime(microsecs /
1000))
)# 微秒
time_start =
920581705000
time_end =
1158045505000
print
(secs2datestr(time_start)
)# 1999-03-05 05:08:25
print
(secs2datestr(time_end)
)# 2006-09-12 15:18:25
三、將乙個日期變成微秒
def
datestr2secs
(datestr)
: tmlist =
array = datestr.split(
' ')
array1 = array[0]
.split(
'-')
array2 = array[1]
.split(
':')
for v in array1:
int(v)
)for v in array2:
int(v))0
)0)0
) tmlist =
tuple
(tmlist)
iflen
(tmlist)!=9
:return
0return
int(time.mktime(tmlist)
)print
(datestr2secs(
'2002-01-09')*
1000
)# 1010505600000
import datetime
import time
print
(time.time())
now = datetime.datetime.now(
)print
(now)
struct = datetime.datetime(now.year,now.month,now.day,now.hour,now.minute,now.second)
print
(time.mktime(struct.timetuple())
)
時間的獲取方法
sql code declare dtdatetime set dt getdate declare number intset number 3 1 指定日期該年的第一天或最後一天 a.年的第一天 select convert char 5 dt 120 1 1 b.年的最後一天 select c...
時間獲取方法
獲取當前時間的整點 import datetime today datetime.datetime.now replace minute 0,second 0,microsecond 0 strftime y m d h m s print today 獲取當前時間昨日的整點 yesterday d...
python 獲取時間
conding utf8 from datetime import datetime,timedelta import time a datetime.now print a.strftime y m d h m s 獲取當前時間精確到秒數 2020 12 31 17 23 20 print a.s...