Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random ISO8601 date with python?

I have seen how to go from ISO formatted date such as 2007-01-14T20:34:22+00:00, to a a more readble format with python datetime.

Is there an easy way to generate a random ISO8601 date similar to this answer for datetime strings?

like image 403
user3783608 Avatar asked Aug 11 '14 15:08

user3783608


1 Answers

I'd suggest using this Faker library.

pip install faker

It's a package that allows you to generate a variety of randomised "fake" data sets. Using Faker below generates a random datetime object which you can then convert to an ISO8601 string format using the datetime.datetime.isoformat method.

import faker

fake = faker.Faker()
rand_date = fake.date_time()  # datetime(2006, 4, 30, 3, 1, 38)
rand_date_iso = rand_date.isoformat()  # '2006-04-30T03:01:38'
like image 146
Ffisegydd Avatar answered Sep 22 '22 18:09

Ffisegydd