Python3計算今天是否生日以及距離生日還有多少天

#!/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 (換成自己路徑)

運行效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章