使用python生成一个随机的ISO8601日期? [英] Generate a random ISO8601 date with python?

查看:56
本文介绍了使用python生成一个随机的ISO8601日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了如何从ISO格式的日期(例如 2007-01-14T20:34:22 + 00:00 )转换为使用python datetime .

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.

是否有一种简便的方法来生成随机ISO8601日期,类似于此答案 datetime 字符串?

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

推荐答案

我建议使用此 Faker 图书馆.

pip install faker

这是一个软件包,可让您生成各种随机的伪"数据集.使用下面的 Faker 生成一个随机的 datetime 对象,然后您可以使用

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'

这篇关于使用python生成一个随机的ISO8601日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆