Svet GEoff
Svet GEoff

Reputation: 85

Python Related Question - User Input Date /N00B/

here is my little Python project:

from datetime import date

a_date = date(2020,5,1) 
b_date = date.today()
delta = b_date - a_date

print("Start date:", a_date)
print("Today is day:", delta.days +1)

if int(delta.days) <= 30:
  print("Phase: Vegetation")
if int(delta.days) >= 30:
  print("Phase: Flowering")

My Questions is: How can I make the user to input "a_date"?

Thanks

Upvotes: 0

Views: 67

Answers (2)

codewelldev
codewelldev

Reputation: 246

You can use input() and datetime.date.fromisoformat()

For Python 3.7 and later:

from datetime import date

a_date = date.fromisoformat(input("Enter a date (YYYY-MM-DD): "))
b_date = date.today()
delta = b_date - a_date

print("Start date:", a_date)
print("Today is day:", delta.days +1)

if int(delta.days) <= 30:
  print("Phase: Vegetation")
if int(delta.days) >= 30:
  print("Phase: Flowering")

For Python version 3.6 and earlier:

from datetime import date

def parse_date(date_input):
    return date(int(a_date_input[:4]), int(a_date_input[5:7]), int(a_date_input[8:]))
    
a_date_input = input("Enter a date (YYYY-MM-DD): ")
a_date = parse_date(a_date_input)
b_date = date.today()
delta = b_date - a_date

print("Start date:", a_date)
print("Today is day:", delta.days +1)

if int(delta.days) <= 30:
  print("Phase: Vegetation")
if int(delta.days) >= 30:
  print("Phase: Flowering")

Upvotes: 1

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2685

You can do it by using a python built it function input(). Between the brackets, I have entered the text that will be printed to a user.

from datetime import date

a_date = input('Enter a date')
b_date = date.today()
delta = b_date - a_date

print("Start date:", a_date)
print("Today is day:", delta.days +1)

if int(delta.days) <= 30:
  print("Phase: Vegetation")
if int(delta.days) >= 30:
  print("Phase: Flowering")

Upvotes: 1

Related Questions