围绕日期时间导入的混淆(字符串和日期)
Confusion surrounding datetime import (Strings & Dates)
我真的被我的代码的这一部分困住了,如果能从其他人那里得到一些输入,那就太棒了。这部分不是我写的,我也无法联系写这部分的人(这是一个小组项目)。当您没有正确格式化输入时它确实会出现错误(我们不需要为这个项目做验证所以我不会担心),但我担心的是这些错误:
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'
非常感谢任何帮助<3 我真的不知道从哪里开始这部分代码。顺便说一下代码:
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)
您正在尝试将出生月份读取为 birthday.month
,其中生日是您从用户那里读取的字符串,并且由于该字符串类型没有与之关联的月份属性,因此您得到错误。您可能打算使用的是转换后的日期时间对象 birthdate
.
birthmonth = birthdate.month
我真的被我的代码的这一部分困住了,如果能从其他人那里得到一些输入,那就太棒了。这部分不是我写的,我也无法联系写这部分的人(这是一个小组项目)。当您没有正确格式化输入时它确实会出现错误(我们不需要为这个项目做验证所以我不会担心),但我担心的是这些错误:
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'
非常感谢任何帮助<3 我真的不知道从哪里开始这部分代码。顺便说一下代码:
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)
您正在尝试将出生月份读取为 birthday.month
,其中生日是您从用户那里读取的字符串,并且由于该字符串类型没有与之关联的月份属性,因此您得到错误。您可能打算使用的是转换后的日期时间对象 birthdate
.
birthmonth = birthdate.month