How to use "dateutil(Dates&Time)" with input?

I would like to get the dates in the next year from input.
I tried this but didn’t work.
Thank you so much in advance!!


My code looks like this
d = input(int(“”))
ny = d + relativedelta.relativedelta(years=1)

print()
print(“Sista check \t%s” % d)
print(“Next check \t%s” % ny)

Well - you can’t convert '' to int because it isn’t one.

2 Likes

Instead of input(int("")) try putting in int(input()). You want to turn the input into and integer, not ask for an input using int("") as a parameter.

2 Likes

Thank you for the advice!
I tries this but didn’t work, is there any other ways to get the dates of the next year from input?

try this… you need to to modify the original code to accept a date input in the format “YYYY-MM-DD”, you can use the strptime method from the datetime module

you should also add exceptions for dumb users not following instructions such as inputting dat-month-year or using slashes “/” instead of dash “-”

try this:

from datetime import datetime
from dateutil import relativedelta

def parse_date(date_string):
    # This function takes a date string in the format "YYYY-MM-DD" and returns a date object.
    try:
        parsed_date = datetime.strptime(date_string, "%Y-%m-%d").date()
        
        # Check if the input has the correct date format (year, month, day) and the correct separator
        if date_string == parsed_date.strftime("%Y-%m-%d") and "-" in date_string:
            return parsed_date
        else:
            raise ValueError
    except ValueError:
        print("Invalid date format. Please use the correct dash format: YYYY-MM-DD.")
        return None

print("Du kan kolla när du ska ha service!")

# Parse the initial date string and calculate the next check date
d = parse_date("2023-04-20")
if d is not None:
    ny = d + relativedelta.relativedelta(years=1)
    print()
    print(f"Sista check: {d}")
    print(f"Next check: {ny}")

# Ask the user for their last check date
user_date_input = input("Enter the date of your last check (YYYY-MM-DD): ")
user_date = parse_date(user_date_input)

# If the input is valid, calculate and print the next check date
if user_date is not None:
    next_check = user_date + relativedelta.relativedelta(years=1)
    print()
    print(f"Sista check: {user_date}")
    print(f"Next check: {next_check}")
else:
    print("Invalid input. Unable to calculate the next check date.")

you may need to run it from shell as the console will interpret certain things like slashes badly.

https://replit.com/@vincegeisler/date-check-example?v=1

3 Likes

I do really appreciate your time for the perfect code! I had no idea how to achieve this just by myself! Have a nice day!!!

1 Like

Just an old coder from the assembler/ cobol/fortran /c days. We always had to assume dumb users back then because if you didn’t really bad things happened.

we had a saying
“to Err is human, to really F&^K things up requires a computer and user interaction”

Glad it works for you.

1 Like

Sounds very cool that you’re saying "that saying " from your rich experience!! Thank you again!!!

Isn’t the else here useless? Since you’re returning.

1 Like

good catch. The else statement is not necessary since the function will either return parsed_date or raise a ValueError.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.