time和datetime的具體使用

from datetime import datetime, date
import time

print("獲取當(dāng)天的日期戳:")
print(time.time())  # 獲取時(shí)間戳,包括小數(shù)點(diǎn)后七位
print(int(time.time()))  # 只取整數(shù)部分
# print(time.sleep(3))
print(time.mktime(datetime.today().timetuple()), end="\n\n")

print("獲取當(dāng)前的日期時(shí)間對(duì)象,  date  time 包括秒數(shù)的小數(shù)點(diǎn)后6位:")
print(datetime.now())
print(datetime.today(),end="\n\n")

# time.struct_time(tm_year=2019, tm_mon=3, tm_mday=3, tm_hour=15, tm_min=23, tm_sec=36, tm_wday=6, tm_yday=62, tm_isdst=-1)
print("構(gòu)建日期時(shí)間的元組對(duì)象:")
now_time_tuple = datetime.today().timetuple()
print(now_time_tuple)
print(now_time_tuple.tm_year, end="\n\n")  # 獲取指定屬性

print("時(shí)間元組和時(shí)間戳相互轉(zhuǎn)換:")
print(time.localtime(time.time()))
print(time.mktime(datetime.today().timetuple()), end="\n\n")  # 時(shí)間元組轉(zhuǎn)換為時(shí)間戳

print("構(gòu)建指定日期的日期對(duì)象:")
datetime_obj = datetime(year=2019, month=3, day=3, hour=15, minute=32, second=34)  # microsecond屬性不添加
date_obj = date(year=2019, month=3, day=3)  # microsecond屬性不添加
print(date_obj)
print(datetime_obj)
print(datetime_obj.year,end="\n\n")    # 獲取日期的屬性

print("獲取當(dāng)前的日期對(duì)象:")
print(datetime.now())
print(datetime.fromtimestamp(time.time()))   # 將時(shí)間戳轉(zhuǎn)換為日期
print(date.today(), end="\n\n")

print("時(shí)間戳和日期之間的相互轉(zhuǎn)換:")
timestamp = time.time()
print(datetime.fromtimestamp(timestamp))   # 將時(shí)間戳轉(zhuǎn)換為日期
print(time.localtime(timestamp))
print(datetime_obj.timestamp(), end="\n\n")  # 將日期對(duì)象轉(zhuǎn)換為時(shí)間戳


print("將日期對(duì)象轉(zhuǎn)換為字符串  , 將字符串轉(zhuǎn)換為日期對(duì)象:")
datetime_now = datetime.now()
datetime_str = datetime.strftime(datetime_now, "%Y-%m-%d")
print(datetime_str)
print(type(datetime_str))

datetime_obj = datetime.strptime(datetime_str, "%Y-%m-%d")
print(datetime_obj)
print(type(datetime_obj))

結(jié)果如下:

創(chuàng)新互聯(lián)建站主營黔西南州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),黔西南州h5微信平臺(tái)小程序開發(fā)搭建,黔西南州網(wǎng)站營銷推廣歡迎黔西南州等地區(qū)企業(yè)咨詢

獲取當(dāng)天的日期戳:
1551603197.907182
1551603197
1551603197.0

獲取當(dāng)前的日期時(shí)間對(duì)象,  date  time 包括秒數(shù)的小數(shù)點(diǎn)后6位:
2019-03-03 16:53:17.907182
2019-03-03 16:53:17.907182

構(gòu)建日期時(shí)間的元組對(duì)象:
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=3, tm_hour=16, tm_min=53, tm_sec=17, tm_wday=6, tm_yday=62, tm_isdst=-1)
2019

時(shí)間元組和時(shí)間戳相互轉(zhuǎn)換:
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=3, tm_hour=16, tm_min=53, tm_sec=17, tm_wday=6, tm_yday=62, tm_isdst=0)
1551603197.0

構(gòu)建指定日期的日期對(duì)象:
2019-03-03
2019-03-03 15:32:34
2019

獲取當(dāng)前的日期對(duì)象:
2019-03-03 16:53:17.907182
2019-03-03 16:53:17.907182
2019-03-03

時(shí)間戳和日期之間的相互轉(zhuǎn)換:
2019-03-03 16:53:17.907182
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=3, tm_hour=16, tm_min=53, tm_sec=17, tm_wday=6, tm_yday=62, tm_isdst=0)
1551598354.0

將日期對(duì)象轉(zhuǎn)換為字符串  , 將字符串轉(zhuǎn)換為日期對(duì)象:
2019-03-03
<class 'str'>
2019-03-03 00:00:00
<class 'datetime.datetime'>

標(biāo)題名稱:time和datetime的具體使用
網(wǎng)站鏈接:http://muchs.cn/article16/gciodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、搜索引擎優(yōu)化品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營銷推廣網(wǎng)站營銷、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管