【问题标题】:Change joined_at timezone discord.py改变joined_at时区discord.py
【发布时间】:2019-02-24 23:32:10
【问题描述】:

我尝试了很多涉及pytz 的方法来更改discord.py user.joined_at 时间的时区。它在 UTC 中,我需要它在 EST 中。

我做的是

eastern = timezone('US/Eastern')
eastern.zone
fmt='%#c
jointime=eastern.localize(datetime(user.joined_at))
createtime=eastern.localize(datetime(user.created_at))
embed=discord.Embed(title='User Information:', color=0x0000ff)
embed.set_author(name='{0.name}'.format(user),icon_url='{0.avatar_url}'.format(user))
embed.add_field(name='Join Date:', value='{0.name} joined on'.format(user)+jointime.strftime(fmt))
embed.add_field(name='Account Creation:', value='{0.name}\'s account was created on '.format(user)+createtime.strftime(fmt))

【问题讨论】:

    标签: python python-3.x datetime timezone discord.py


    【解决方案1】:

    使用 pytz 模块

    from pytz import timezone
    
    current_timezone_time = ctx.message.author.joined_at
    new_timezone_time = current_timezone_time.astimezone(timezone('US/Pacific'))
    #do whatever
    

    在本例中,我已将其转换为 US/Pacific,但您可以使用 whichever one you want

    【讨论】:

    • 我试过了,但由于某种原因它没有奏效
    • 根据它的生成方式,您可能需要提醒datetime 对象它是UTC,然后才能转换它ctx.message.author.joined_at.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Eastern'))
    • @PatrickHaugh thx,datetime 对象不在 UTC,所以现在我用 EST 修复了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2016-06-21
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多