lundi 30 novembre 2020

Returning dates in different format in python

I have made a function in the format: day/month/year. I would like to make another function that asks the user for example: "When do you want to come?" and depending on what month the user say I want to print out a answer.

The problem: I don't know how to make the function that asks the user for the format: day/month/year without limit the years, I would like the user to enter any year and still be able to get the same answers as another any year (but different month).

enter code here
import datetime 

def dateformat(date):
    return datetime.datetime.strptime(datumet, "%d/%m/%Y")

def ask_user():
    winter = dateformat('1/1/2020') <= dateformat('31/3/2020')
    spring = dateformat('1/4/2020') <= dateformat('31/5/2021')
    summer = dateformat('1/6/2020') <= dateformat('31/9/2021')
    autumn = dateformat('1/10/2020') <= dateformat('31/12/2021')
    
    a = dateformat(input("When do you want to come"))
    if a == winter:
        print("Hi")
    if a == spring:
        print("bye")
    if a == summer:
        print("ok")
    if a == autumn:
        print("no")

My question: How can I make this code work for any year? I would like to be able to type any year but inside the month and get the same output. If I only return %d/%m in the dateformat-function the user will not be able to type: day/month/year. Is there maybe a better way of returning the format?

Aucun commentaire:

Enregistrer un commentaire