简体   繁体   中英

Change Date to friendly format in Python

I'm trying to change the date of launch time for EC2 instances in AWS to something more friendly using Python 3.

The error that I'm getting says:

datetime(launch_time)
TypeError: 'module' object is not callable

My program is doing this:

import boto3
import time
import datetime

instance_id = 'i-024b3382f94bce588'
instance = ec2.describe_instances(
    InstanceIds=[instance_id]
)['Reservations'][0]['Instances'][0]
launch_time = instance['LaunchTime']
datetime(launch_time)
launch_time_friendly = launch_time.strftime("%B %d %Y")
print("Server was launched at: ", launch_time_friendly)

How can I get the time the instances were created into a user friendly format?

There is both a datetime module and a datetime class. You are attempting to call the module:

import datetime
dt = datetime(2019, 3, 1)  # This will break!

Instead, you need to either import the class from the module:

from datetime import datetime
dt = datetime(2019, 3, 1)  # Okay!

... or import the module and reference the class:

import datetime
dt = datetime.datetime(2019, 3, 1)  # Good!

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