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)建站主營(yíng)黔西南州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP軟件開(kāi)發(fā),黔西南州h5微信平臺(tái)小程序開(kāi)發(fā)搭建,黔西南州網(wǎng)站營(yíng)銷(xiāo)推廣歡迎黔西南州等地區(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-032019-03-03 00:00:00