python31[时间和日期]
时间:2011-01-15 来源:iTech
#class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
oneyear = datetime.timedelta(days=365)
fivehours = datetime.timedelta(hours =2)
tenminutes = datetime.timedelta(minutes = 10)
all = oneyear + fivehours + tenminutes
print(all)
#365 days, 2:10:00
today = datetime.date.today()
print(today)
#2011-01-15
print(today.year)
#2011
print(today.month)
#1
print(today.day)
#15
print(today.weekday())
#5 # Monday is 0 and Sunday is 6
print(today.isoweekday())
#6 # Monday is 1 and Sunday is 7
print(today.toordinal())
#734152 #the days from 0001.01.01
tomorrow = today.fromordinal(today.toordinal() + 1)
print(tomorrow)
#2011-01-16
print(today.isoformat())
#2011-01-15
print(today.timetuple()[0])
#2011
print(today.timetuple()[1])
#1
print(today.timetuple()[2])
#15 aftertenyears = today.replace(year = 2021)
print(aftertenyears)
#2021-01-15
date1 = datetime.date(2010,10,10)
date2 = datetime.date(2010,9,20)
diff = date1 - date2 # Diff is datetime.timedelta type
print(diff)
#20 days, 0:00:00
if date1 > date2 :
print("date1 > date2")
#date1 > date2
time1 = datetime.time(13,30,20)
print(time1)
#13:30:20
print(time1.hour)
#13
print(time1.minute)
#30
print(time1.second)
#20
print(time1.microsecond)
#0
print(time1.isoformat())
#13:30:20
time2 = time1.replace(second = 30)
print(time2)
#13:30:30
oneyear = datetime.timedelta(days=365)
fivehours = datetime.timedelta(hours =2)
tenminutes = datetime.timedelta(minutes = 10)
all = oneyear + fivehours + tenminutes
print(all)
#365 days, 2:10:00
二 日期
#datetime.date(year, month, day)today = datetime.date.today()
print(today)
#2011-01-15
print(today.year)
#2011
print(today.month)
#1
print(today.day)
#15
print(today.weekday())
#5 # Monday is 0 and Sunday is 6
print(today.isoweekday())
#6 # Monday is 1 and Sunday is 7
print(today.toordinal())
#734152 #the days from 0001.01.01
tomorrow = today.fromordinal(today.toordinal() + 1)
print(tomorrow)
#2011-01-16
print(today.isoformat())
#2011-01-15
print(today.timetuple()[0])
#2011
print(today.timetuple()[1])
#1
print(today.timetuple()[2])
#15 aftertenyears = today.replace(year = 2021)
print(aftertenyears)
#2021-01-15
date1 = datetime.date(2010,10,10)
date2 = datetime.date(2010,9,20)
diff = date1 - date2 # Diff is datetime.timedelta type
print(diff)
#20 days, 0:00:00
if date1 > date2 :
print("date1 > date2")
#date1 > date2
三 时间
#datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)time1 = datetime.time(13,30,20)
print(time1)
#13:30:20
print(time1.hour)
#13
print(time1.minute)
#30
print(time1.second)
#20
print(time1.microsecond)
#0
print(time1.isoformat())
#13:30:20
time2 = time1.replace(second = 30)
print(time2)
#13:30:30
四 时间和日期
完!
相关阅读 更多 +