python模块——时间

一、时间模块

1、time模块中常见的方法:

time() #1970到现在的经过的秒数

ctime() #固定格式的当前时间

sleep(3) #休眠 单位是秒

asctime() #转换为asc码显示当前时间

strftime() #时间格式化

import time print(time.time()) #1970年到现在经过的秒数

print(time.ctime()) #固定格式的当前时间

print(1) time.sleep(5) #休眠5s,强制等待

print(2) print(time.asctime()) #转化为asc码显示当前时间

print(time.strftime("%H-%M-%S")) #时分秒

print(time.strftime("%y-%m-%d")) #年月日

print(time.strftime("%H-%M-%S %y-%m-%d"))#时间戳:按照格式输出内容:时分秒,年月日 ![](https://img2024.cnblogs.com/blog/3603791/202503/3603791-20250317191530574-1128278893.png)

2、datetime

18、输入某年某月某日,判断这一天是这一年的第几天
import datetime
year,month,day=map(int,input().split(" "))

yuandan=datetime.datetime(year,1,1)
now=datetime.datetime(year,month,day)

print((now-yuandan).days+1)

 

posted @ 2025-04-06 16:58  君庭  阅读(17)  评论(0)    收藏  举报