datetime模块/time模块


datetime实例的方法

from datetime import *
print(datetime.now())           #返回当天的日期和时间
today=datetime.now()            #定义today为当天日期时间对象
print(datetime.date(today))     #返回当天的日期对象
print(datetime.time(today))     #返回当天的时间对象
print(datetime.ctime(today))    #返回"星期 月 日 时 分 秒 年 "格式的字符串
print(datetime.utcnow())        #返回当天的UTC日期和时间  datetime类型
print(datetime.timestamp(today))     #返回当天的时间戳(UNIX时间戳),浮点数类型
print(datetime.fromtimestamp(datetime.timestamp(today)))  #根据时间戳返回UTC日期时间,datetime类型
date1=date(2018,2,12)               #使用date类 实例化date1
time1=time(20,53,48)
print(datetime.combine(date1,time1))

# 2020-11-15 17:24:03.775633
# 2020-11-15
# 17:24:03.775633
# Sun Nov 15 17:24:03 2020
# 2020-11-15 09:24:03.775633
# 1605432243.775633
# 2020-11-15 17:24:03.775633
# 2018-02-12 20:53:48

strftime()方法和strptime()方法的时间日期格式化符号

参考[:https://blog.csdn.net/chenlei0630/article/details/41802719]

time模块类似,只列出部分特有

import time

# time.sleep(5)           #执行该代码时,执行的程序线程将暂停5s.
#t1=time.clock()            #Python3.8不再支持time.clock,但在调用时依然包含该方法
#==========用time.perf_counter方法替换

tis1 =time.perf_counter()
time.sleep(5)
tis2 =time.perf_counter()
print(tis2-tis1)

now_time=time.strftime('下午%H时,%M分,%S秒')
print(now_time)

tis3=time.time()            #返回资纪元年起的秒数
print(tis3)

#============================执行结果
# 4.999899
# 下午20时,59分,13秒
# 1605445153.0820704