简体   繁体   English

给出月份中星期几的程序

[英]program which gives the day of the week of day of the month

I would like to write a program that gives the day of the week on the 30th of the month.我想编写一个程序,在每个月的 30 号给出星期几。 I have a code in which I have to specify the date, what can I change in it to work specifically for the 30th of the month?我有一个代码,我必须在其中指定日期,我可以在其中更改哪些内容以专门针对该月的 30 日工作?

import datetime
date=str(input('(Date for example 30 03 2019):'))
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%d %m %Y').weekday()
print(day_name[day])

try this:尝试这个:

from datetime import datetime
year = int(input("Please enter year:"))
month = int(input("Please enter month:"))
try:
    print(datetime(year,month,30).strftime("%A"))
except ValueError as ex:
    # in February there is no 30 days
    print(f"No such date - {year}-{month}-30")

Keep in mind that you have to think about another logic for February...请记住,您必须考虑二月份的另一个逻辑......

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

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