Python3计算今天是否生日以及距离生日还有多少天
#!/usr/bin/python# -*- coding: utf-8 -*-"""Author:LQYEmail:xiaoxiazuozhu@163.comdate:2020-06-28 13:40IDE: PyCharm"""import datetimeimport time#年月日单个toyear = time.strftime('%Y', time.localtime(time.tim
·
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import time
# 年月日单个
toyear = time.strftime('%Y', time.localtime(time.time()))
tomon = time.strftime('%m', time.localtime(time.time()))
todayy = time.strftime('%d', time.localtime(time.time()))
toyear = int(toyear)
tomon = int(tomon)
todayy = int(todayy)
# 年月日合起来
today = time.strftime("%Y-%m-%d", time.localtime())
today_mon_day = time.strftime("%m-%d", time.localtime())
print("今天是: " + today_mon_day)
# 获取年
def insert_year():
# 2月闰年29天,不是闰年就是28天
flag = True
while flag:
input_year = input("输入出生年份 : ")
input_year = int(input_year)
# 今年之前出生的
if input_year <= toyear:
return input_year
flag = False
# 今年出生的
else:
print("请不要输入未来的年份")
continue # 今年之前出生的
# 获取月
def insert_mon():
flag = True
while flag:
input_mon = input("输入月份: ")
input_mon = int(input_mon)
if input_mon > 12 or input_mon < 1:
print("输入正确的数字")
continue
else:
return input_mon
flag = False
def insert_day():
flag = True
while flag:
input_day = input("输入日 :")
input_day = int(input_day)
if input_day > todayy or input_day > 31 or input_day < 1:
print("请输入正确的日份")
continue
elif input_day == todayy:
print("生日快乐")
flag = False
return input_day
else:
return input_day
flag = False
# 计算还有多少天生日(生日\今天\生日月\生日天)
def how_long(today, mon, day):
try:
# 明年的今天
next_year = int(toyear) + 1
str3 = str(next_year) + "-" + str(mon) + "-" + str(day)
str4 = str(int(toyear)) + "-" + str(mon) + "-" + str(day)
date2 = datetime.datetime.strptime(today[0:10], "%Y-%m-%d") # 今天
date3 = datetime.datetime.strptime(str3[0:10], "%Y-%m-%d") # 明年生日=今年年份+1 +生日的月日
date4 = datetime.datetime.strptime(str4[0:10], "%Y-%m-%d") # 今年的年+生日的月日
num = 0
# 明年
# 今天过生日:月日相等
if mon == tomon:
if day == todayy:
print("今天过生日,祝你生日快乐")
num = 0
if day > todayy:
print("这个月过生日")
num = (date4 - date2).days
if day < todayy:
print("生日这个月已经过了")
num = (date3 - date2).days
# 已经过了生日的:明年生日-今天
elif mon < tomon:
print("今年生日已经过了")
num = (date3 - date2).days
# 还没过生日:今年的年+生日的月日 - 今天的年月日
else:
print("今年的生日还没到")
num = (date4 - date2).days # 返回的全部是非0的整数
except ValueError as e:
print("请输入正确的日期,一个月只有适合的天数 " + e)
print("程序结束...")
return num
if __name__ == "__main__":
year = insert_year()
mon = insert_mon()
day = insert_day()
num = how_long(today, mon, day)
if num != 0:
print("距离生日还有" + str(num) + "天")
mybirthday = str(year) + "-" + str(mon) + "-" + str(day)
print("你的生日是: " + mybirthday)
print("输入任意按键退出~")
input()
打包:
pyinstaller -F D:\project\test.py (换成自己路径)
运行效果:
更多推荐
已为社区贡献8条内容
所有评论(0)