简体   繁体   中英

How to fix datetime TypeError in python

I'm pulling cert information out of IAM in AWS and trying to format the expiration date. However, when trying to slice up the date, i'm getting the error TypeError: 'datetime.datetime' object has no attribute ' getitem '

I'm sure there is probably an obvious fix, but i'm not having much luck.

conn = boto3.client('iam')
iamcerts = conn.list_server_certificates()
response = iamcerts['ServerCertificateMetadataList']
for i in response:
    year  = int(i['Expiration'][0:4])
    month = int(i['Expiration'][5:7])
    day = int(i['Expiration'][8:10])
    exp = date(year, month, day)
    daysleft = exp - today

File "test2.py", line 39, in year = int(i['Expiration'][0:4]) TypeError: 'datetime.datetime' object has no attribute ' getitem '

What I wanted to do was slice off the year, month and day and assign them to the corresponding variables in order to find out how much time is left until the cert expires.

Got the desired outcome using the following:

datestring = i['Expiration']
year  = int(datestring.year)
month = int(datestring.month)
day = int(datestring.day)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM