snowbuns
snowbuns

Reputation: 31

Confusion surrounding datetime import (Strings & Dates)

I'm really stuck on this part of my code and it would be amazing to get some input from somebody else. I didn't write this section and I am unable to contact the person who did (it is a group project). It does come up with errors when you don't format the input correctly (we are not required to do validations for this project so I would not worry about that), but what I am concerned about are these errors:

Traceback (most recent call last):
  File "/Users/mycomputer/PycharmProjects/project/main.py", line 46, in <module>
    stringsdates()
  File "/Users/mycomputer/PycharmProjects/project/main.py", line 29, in stringsdates
    birthmonth = birthday.month
AttributeError: 'str' object has no attribute 'month'

Any help is greatly appreciated <3 I really have no idea where to start in this section of code. Here is the code by the way:

import datetime

def yearadd(date):
    test = datetime.timedelta(days=365)
    return date + test

def stringsdates():
    # Inputs for first and last name, start date and birthday along with yearly salary.

    firstname = input("What is your first name? ")
    lastname = input("What is your last name? ")
    startdate = input("What is the date you started with the company? (YYYY MM DD) ")
    birthday = input("What is your birthday? (YYYY MM DD) ")
    yearlysal = input("What is your yearly salary? ")

    # ♡ Processing
    startdate = datetime.datetime.strptime(startdate, "%Y %m %d")
    birthdate = datetime.datetime.strptime(birthday, "%Y %m %d")
    yearhired = startdate.year
    birthmonth = birthday.month
    today = datetime.datetime.today()

    formatfullname = (firstname + lastname + ", " + firstname[0] + "." + lastname + ", " + lastname + ", " + firstname[
        0] + ".")
    employeenum = firstname.upper()[0] + lastname.upper()[0] + "-" + str(yearhired) + "-" + str(birthmonth)
    reviewdate = yearadd(yearhired)
    nextbday = today - birthdate

    # ♡ Output
    print(formatfullname)
    print(employeenum)
    print(reviewdate)
    print(nextbday)
    print(yearlysal)

Upvotes: 0

Views: 77

Answers (1)

phoney_badger
phoney_badger

Reputation: 533

You are trying to read the birthmonth as birthday.month where birthday is a string which you are reading in from the user, and as the string type has no month attribute associated with it, you're getting the error. What you probably meant to use was the converted datetime object birthdate.

birthmonth = birthdate.month

Upvotes: 1

Related Questions