将用户指定的日期添加到现有代码
Adding a user specified date to existing code
我是一个但坚持能够将用户定义的日期添加到这个 Days to Go 代码。适用于嵌入的设定日期。但是不能让它与输入线一起工作。
from datetime import datetime, time
b = input
event = (input('What is the name of your event?')) # input the name of the event
year = int(input('Enter a year')) # input the requires year
month = int(input('Enter a month')) # input the required month
day = int(input('Enter a day')) # input the required day
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime.date(b[1], b[2], b[3])
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
我认为你的问题很可能是这一行:
date1 = datetime.date(b[1],b[2],b[3])
尝试将其更改为:
date1 = datetime.date(year, month, day, 0, 0, 0)
首先,你用错了b=input
。意思是你要用函数名b的input
函数,比如event = b('what is the name of your event?')
.
相反,您可以在使用 input()
.
获取信息后将值赋给 b
,例如 b = (event, year, month, day)
并且您通过 from datetime import datetime
导入了 datetime
模块,您不需要明确地说 datetime.date
,只需 date
。不过,这里可以使用datetime
而不是date
,如下:
from datetime import datetime, time
#b = input -> wrong usage
event = (input('What is the name of your event? ')) # input the name of the event
year = int(input('Enter a year ')) # input the requires year
month = int(input('Enter a month ')) # input the required month
day = int(input('Enter a day ')) # input the required day
b = (event, year, month, day) # you can assign date values to b
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime(b[1], b[2], b[3]) # not datetime.date()
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
# if you want to print the event together:
print("\n%d days, %d hours, %d minutes, %d seconds left for %s" % (
dhms_from_seconds(date_diff_in_seconds(date2, date1)) + (event,)))
结果是这样的:
What is the name of your event? birthday
Enter a year 2022
Enter a month 03
Enter a day 19
0 days, 14 hours, 40 minutes, 2 seconds
0 days, 14 hours, 40 minutes, 2 seconds left for Sunday # in case that you print the event together
我是一个但坚持能够将用户定义的日期添加到这个 Days to Go 代码。适用于嵌入的设定日期。但是不能让它与输入线一起工作。
from datetime import datetime, time
b = input
event = (input('What is the name of your event?')) # input the name of the event
year = int(input('Enter a year')) # input the requires year
month = int(input('Enter a month')) # input the required month
day = int(input('Enter a day')) # input the required day
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime.date(b[1], b[2], b[3])
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
我认为你的问题很可能是这一行:
date1 = datetime.date(b[1],b[2],b[3])
尝试将其更改为:
date1 = datetime.date(year, month, day, 0, 0, 0)
首先,你用错了b=input
。意思是你要用函数名b的input
函数,比如event = b('what is the name of your event?')
.
相反,您可以在使用 input()
.
b
,例如 b = (event, year, month, day)
并且您通过 from datetime import datetime
导入了 datetime
模块,您不需要明确地说 datetime.date
,只需 date
。不过,这里可以使用datetime
而不是date
,如下:
from datetime import datetime, time
#b = input -> wrong usage
event = (input('What is the name of your event? ')) # input the name of the event
year = int(input('Enter a year ')) # input the requires year
month = int(input('Enter a month ')) # input the required month
day = int(input('Enter a day ')) # input the required day
b = (event, year, month, day) # you can assign date values to b
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime(b[1], b[2], b[3]) # not datetime.date()
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
# if you want to print the event together:
print("\n%d days, %d hours, %d minutes, %d seconds left for %s" % (
dhms_from_seconds(date_diff_in_seconds(date2, date1)) + (event,)))
结果是这样的:
What is the name of your event? birthday
Enter a year 2022
Enter a month 03
Enter a day 19
0 days, 14 hours, 40 minutes, 2 seconds
0 days, 14 hours, 40 minutes, 2 seconds left for Sunday # in case that you print the event together