Python date测试

import datetime, calendar

today = datetime.date.today()
oneday = datetime.timedelta(days=1)

# loop back to most recent Friday (could be today)
# takes care of end of month and even leap years

friday = today
while friday.weekday() != calendar.FRIDAY:
friday -=oneday

oneweek = datetime.timedelta(days=7)
nextweek = today + oneweek
nextyear = today.replace(year=today.year+1)

print "Today (year-month-day) = ", today
print "Most recent Friday = ", friday
print "One week from today = ", nextweek
print "One year from today = ", nextyear
print "Current time = ", datetime.datetime.now().time() # just the time
print "Lunchtime = ", datetime.time(12,00) #12:00:00

print '-'*50

# a typical birthday year, month, day
birthday = datetime.date(1977,4,27)
# three diffeerent ways to present a date
# 1977-04-27
print "Birthday format1: ", birthday
# 04/27/77
print "Birthday format2: ", birthday.strftime("%m/%d/%y")
# 01Apr1977
print "Birthday format3: ", birthday.strftime("%d %b %Y")

age = today - birthday
print "You are ", age.days, "days old today!"

# extract the year from todays date
thisyear = int(today.strftime("%Y")[0:4])

# party message (include date and time)
rightnow = datetime.datetime.today()
bparty = datetime.datetime(thisyear, 4, 27, 14, 30)
# check if you can still make it to the party ...
if bparty > rightnow:
print "Birthday party", bparty
else:
print "Mised this year's birthday party!"

print '-'*50

# calculate days till xmas of this year
xmas = datetime.date(thisyear, 12, 25)
tillXmas = xmas - today
print "There aere %d days till xmas!" % tillXmas.days

print '-'*50

# add days to a given date
delta = datetime.timedelta(days=77)
addDays = today + delta
print "Today 's date is :", today.strftime("%d%b%Y")
print "77 days from today it will be :", addDays.strftime("%d%b%Y")

print '-'*50

Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']

print "Today is ", Weekday[datetime.date.weekday(today)]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值