以 ISO 日期时间格式 yyyy-MM-dd'T'HH:mm:ss 生成随机日期。 SSXXX

发布于 2025-01-13 21:45:41 字数 194 浏览 0 评论 0原文

这是我第一次在 Python 中处理日期。我试图在 python 中生成一个范围内的随机日期,日期格式应该是: yyyy-MM-dd'T'HH:mm:ss.SSSXXX+timezone 例如,“2022-10-31T01:30:00.000+05:00”

我还需要在生成的日期中添加一个小时整数,我是模拟飞行,因此第一个日期是出发日期,第二个日期是着陆日期。

It's my first time working with dates in Python. I am trying to generate a random date within a range in python the date format should be:
yyyy-MM-dd'T'HH:mm:ss.SSSXXX+timezone for example, "2022-10-31T01:30:00.000+05:00"

I also need to add an hour integer to the generated date, I am simulating a flight, so the first date is the departure and the second one the landing date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑,眼淚并存 2025-01-20 21:45:41

这基本上是两个不同的问题,您可以将它们合并起来以获得结果。我假设您已经有日期时间对象。

获取随机日期

您可以使用另一个问题的答案生成随机日期:

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)

它接收两个日期时间对象。

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)

以所需格式显示日期

您需要使用 .isoformat()。输出

datetime(2019, 5, 18, 15, 17, 0, 0, tzinfo=timezone.utc).isoformat()

2019-05-18T15:17:00+00:00

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
我最亲爱的 2025-01-20 21:45:41

类似这样的事情

from datetime import datetime,tzinfo,timedelta
from faker import Faker


class simple_utc(tzinfo):
    def tzname(self,**kwargs):
        return "UTC"
    def utcoffset(self, dt):
        return timedelta(0)


fake=Faker()
#define your range
randomdate=fake.date_time_between(start_date="-1d", end_date="now")
#add an hour
randomdatePlusHour=randomdate+timedelta(hours=1)

print(randomdate.replace(tzinfo=simple_utc()).isoformat(),randomdatePlusHour.replace(tzinfo=simple_utc()).isoformat())

与 faker 的先决条件(pip install faker)有关。

Something like this

from datetime import datetime,tzinfo,timedelta
from faker import Faker


class simple_utc(tzinfo):
    def tzname(self,**kwargs):
        return "UTC"
    def utcoffset(self, dt):
        return timedelta(0)


fake=Faker()
#define your range
randomdate=fake.date_time_between(start_date="-1d", end_date="now")
#add an hour
randomdatePlusHour=randomdate+timedelta(hours=1)

print(randomdate.replace(tzinfo=simple_utc()).isoformat(),randomdatePlusHour.replace(tzinfo=simple_utc()).isoformat())

with the prerrequisit of faker (pip install faker).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文