【python日期/时间题】

time及datetime函数应用

格式修改 

a = "2013-10-10 23:40:00"想改为 a = "2013/10/10 23:40:00"

import time
a = "2013-10-10 23:40:00"
b = time.strptime(a, "%Y-%m-%d %H:%M:%S")
c = time.strftime("%Y/%m/%d %H:%M:%S", b)
print(b)
#time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
print(c)
#2013/10/10 23:40:00
from datetime import *
dt1=datetime(2023,4,6)
dt2=datetime(2024,1,3)
print('相差多少天:',(dt2-dt1).days)
s='%04d%02d%02d'%(dt1.year,dt1.month,dt1.day)
print('字符串表示为',s)#20210101
print('星期:',dt1.weekday()+1)#星期几,0是星期一

delta=timedelta(1)
while dt1<=dt2:
    dt1+=delta
    print(dt1)

t1 = datetime(2017,3,3,8,49,0)
t2 = datetime(2017,3,3,8,50,30)
print('相差时间:',t2-t1)
print ('相差秒:',(t2 - t1).seconds)


闰年判断

day=[0,31,28,31,30,31,30,31,31,30,31,30,31]
y,m,d,k=map(int,input().split())
if (y%4==0 and y%100!=0) or y%400==0:
    day[2]=29
else:
    day[2]=28

 

你可能感兴趣的:(python,开发语言)