python-3.x - 试图找出两个日期之间的时间,然后让它们在 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 对其进行了编辑,因为总和是错误的,并且截止日期是错误的
解决方案
也许这个功能会帮助你:
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
在你的嵌入中使用你想要的变量。
推荐阅读
- powershell - 忽略 azure 管道中缺少未满足的依赖项警告的纱线
- php - 调试卡在 PhpStorm
- c# - 将类的字符串属性解析为字符串数组
- mysql - 是否可以更改数据库的 mysql 转储文件中的数据类型?
- python - TensorFlow API Slim:如何为 VGG-Net 16 设置 checkpoint_exclude_scopes 和 output_node_names?
- java - Swagger POST 生成的参数正文覆盖
- java - ListView 数据以不寻常的格式显示?
- python-3.x - 从 Python3 将参数传递给 bash 脚本
- html - 如何使引导程序的左列填充到屏幕的整个高度?
- powershell - 从 Intune 静默部署和运行 Offscrub VBS 脚本