首页 > 解决方案 > 试图找出两个日期之间的时间,然后让它们在 python 中可读

问题描述

试图发出一个不和谐的命令来显示我女儿还活着的时间

@commands.command(hidden=True)
@commands.cooldown(1, 5, commands.BucketType.guild)
async def luxe(self, ctx):
    currentDate = datetime.utcnow()
    print(currentDate)
    deadline = '08/05/2020 10:05:00'
    deadlineDate = datetime.strptime(deadline, '%d/%m/%Y %H:%M:%S')
    print(deadlineDate)
    daysLeft = currentDate-deadlineDate

    years = ((daysLeft.total_seconds()) / (365.242 * 24 * 3600))
    yearsInt = int(years)

    months = (years - yearsInt) * 12
    monthsInt = int(months)

    weeks = (years - yearsInt) * 52
    weeksInt = int(weeks)

    days = (months - monthsInt) * (365.242 / 12)
    daysInt = int(days)

    hours = (days - daysInt) * 24
    hoursInt = int(hours)

    minutes = (hours - hoursInt) * 60
    minutesInt = int(minutes)

    seconds = (minutes - minutesInt) * 60
    secondsInt = int(seconds)

    if yearsInt == 0 and monthsInt == 0:
        embed = discord.Embed(title="Luxe Olivia Violet Ellisdon",
                              description=f"She is exactly {weeksInt} weeks, {daysInt} days, {hoursInt} hours, {minutesInt} minutes, {secondsInt} seconds old.",
                              colour=ctx.author.colour,
                              timestamp=datetime.utcnow())
        embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.avatar_url)
        embed.set_footer(text=f'You found an easter egg')
        embed.set_image(url='https://i.ibb.co/M1kb2q0/100868516-10158119041366257-6490464808603746304-n.jpg')
        await ctx.send(embed=embed)
    elif yearsInt == 0:
        embed = discord.Embed(title="Luxe Olivia Violet Ellisdon",
                              description=f"She is exactly {monthsInt} months, {weeksInt} weeks, {daysInt}  days, {hoursInt}  hours, {minutesInt} minutes, {secondsInt} seconds old.",
                              colour=ctx.author.colour,
                              timestamp=datetime.utcnow())
        embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.avatar_url)
        embed.set_image(url='https://i.ibb.co/M1kb2q0/100868516-10158119041366257-6490464808603746304-n.jpg')
        embed.set_footer(text=f'You found an easter egg')
        await ctx.send(embed=embed)
    else:
        embed = discord.Embed(title="Luxe Olivia Violet Ellisdon",
                              description=f"she is exactly {yearsInt} years, {monthsInt} months, {weeksInt }weeks, {daysInt}  days, {hoursInt}  hours, {minutesInt} minutes, {secondsInt} seconds old.",
                              colour=ctx.author.colour,
                              timestamp=datetime.utcnow())
        embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.avatar_url)
        embed.set_image(url='https://i.ibb.co/M1kb2q0/100868516-10158119041366257-6490464808603746304-n.jpg')
        embed.set_footer(text=f'You found an easter egg')
        await ctx.send(embed=embed)

这是输出

它不太正确,我不明白为什么,如果有人能解释我做错了什么,我会很高兴。Ived 对其进行了编辑,因为总和是错误的,并且截止日期是错误的

标签: python-3.xdatetimetimediscord.py

解决方案


也许这个功能会帮助你:

def convert_timedelta(duration):
    days, seconds = duration.days, duration.seconds
    hours = seconds // 3600
    minutes = (seconds % 3600) // 60
    seconds = (seconds % 60)
    return days, hours, minutes, seconds

例子:

# Your code
currentDate = datetime.utcnow()
deadline = '08/05/2020 10:05:00'
deadlineDate = datetime.strptime(deadline, '%d/%m/%Y %H:%M:%S')
daysLeft = currentDate - deadlineDate
days, hours, minutes, seconds = convert_timedelta(daysLeft) # Use function
print('Days left:', days,
      '\nHours left:', hours,
      '\nMinutes left:', minutes,
      '\nSeconds left:', seconds)

输出:

Days left: 42
Hours left: 7
Minutes left: 47
Seconds left: 44

然后你可以days, hours, minutes, seconds在你的嵌入中使用你想要的变量。


推荐阅读