This is basically two different questions which you can join to get your result. I'm assuming you already have datetime objects.
Get a random date
You could generate a random date using an answer from another question:
from random import randrange
from datetime import timedelta
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
which receives two datetime objects.
Usage:
d1 = datetime.strptime('1/1/2008 1:30 PM', '%m/%d/%Y %I:%M %p')
d2 = datetime.strptime('1/1/2009 4:50 AM', '%m/%d/%Y %I:%M %p')
random_date(d1, d2)
Display date on desired format
You need to use .isoformat().
datetime(2019, 5, 18, 15, 17, 0, 0, tzinfo=timezone.utc).isoformat()
Output:
2019-05-18T15:17:00+00:00