简体   繁体   English

如何根据 Python 中的输入找出自定义日期(日历格式)?

[英]How can find out a custom date ( with calendar format ) based on an input in Python?

My problem sounds like this: For N people, input their names and birthday dates.我的问题听起来像这样:对于 N 个人,输入他们的姓名和生日日期。 Based on this information, using substitute, create a custom message like: "Hey... . There are.., more days until your birthday. when you will turn... years old: A trip awaits you in the following days. ... -.., ( the period has to start 3 days before the birthday. and end 3 days after.)根据此信息,使用替代创建一条自定义消息,例如:“嘿……。还有……,还有几天,直到你的生日。当你转……岁时:接下来的几天有一场旅行等着你。 ... -..,(期间必须在生日前 3 天开始。并在生日后 3 天结束。)

For example: n = 1 Steve Potter 02.07.2000例如:n = 1 史蒂夫·波特 02.07.2000

output: Hey Steve Potter, There are 31 more days until your birthday, when you will turn 20 years old: A trip awaits you in the following days. output:嘿,史蒂夫·波特,离你的生日还有 31 天,届时你将满 20 岁:接下来的几天,一场旅行等着你。 29.06.2020 - 05.07.2020 29.06.2020 - 05.07.2020

So far, I can't figure out more than that:到目前为止,我想不出更多:

import string
import time

invitation = string.Template("Hey $name , There are $nrdays days until your birthday, when you will turn $years old. A trip awaits you in the following days: $start - $end")

n = int(input("Number of people:"))

for i in range(0, n):
    i_name = input("Name of the guest?")
    i_data = input("Birthday?")
    data = time.localtime()
    i_bdday = int(i_data[0] + i_data[1] )
    i_bdmonth = int(i_data[3] + i_data[4] )
    i_bdyear = int(i_data[6] + i_data[7] + i_data[8] + i_data[9] )
    i_years = data[0] - i_bdyear
    print ( invitation.substitute(name = i_name , years = i_years) )

How can I figure out how more days are there until a given date and also the interval I need?我如何才能确定距离给定日期还有多少天以及我需要的时间间隔? Thank you谢谢

See this:看到这个:

https://docs.python.org/3/library/datetime.html?highlight=datetime#module-datetime

Maybe you can find your own solution after reading it.或许你可以在阅读后找到适合自己的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM