python给定日期星期几_Python程序检查给定的日期是否有效

python给定日期星期几

To check the given date is valid or not, we will use the datetime module in the program by using the import function and also we will use the try-except statement. For example date 31-02-2020 is invalid because we know that February month has only 28 days in an ordinary year and a leap year it has 29 days but there the given date is 31 so, it is simply an invalid date. Here, we are going to do this work by using the Python programming language. Before going to solve this problem, we will learn the basic syntax of the try-except statement.

为了检查给定的日期是否有效 ,我们将使用import函数在程序中使用datetime模块 ,还将使用try-except语句。 例如,日期31-02-2020无效,因为我们知道二月在普通年中只有28天,而a年有29天,但是给定日期是31,因此,它只是一个无效日期。 在这里,我们将使用Python编程语言来完成这项工作。 在解决此问题之前,我们将学习try-except语句的基本语法。

日期时间模块 (The datetime module)

datetime module is an inbuilt module in Python which provides us to solve various problems related to date and time.

datetime模块是Python中的内置模块,可为我们解决与日期和时间有关的各种问题。

The basic syntax of try-except statement:

try-except语句的基本语法:

    try:
        #statement
    except:
        #statement

  • If the code or statement provided in the try block has no exception then only try executed.

    如果try块中提供的代码或语句没有异常,则仅尝试执行。

  • If any exception occurs in the block of try then try block skipped and except block will be executed.

    如果try块中发生任何异常,则将跳过try块,将执行except块。

Algorithm to solve this problem:

解决此问题的算法:

  1. Initially, we will include the datetime module by using the import function.

    最初,我们将使用导入功能包括datetime模块。

  2. Take the date in the form of the date, month, year.

    以日期,月份,年份的形式输入日期。

  3. Since we know that, we going to check the date is valid or not and if the date is valid then ok but when it's invalid, we will ValueError. So, here we will use the try-except statement.

    既然知道这一点,我们将检查日期是否有效,如果日期有效,则可以,但是当日期无效时,我们将使用ValueError。 因此,这里我们将使用try-except语句。

  4. If the try statement has no exception then we will print the given date is valid otherwise we will print the given date is invalid.

    如果try语句没有异常,则我们将打印给定的日期有效,否则我们将打印给定的日期无效。

Let's see the implementation of the above algorithm in the Python program.

让我们看看上述算法在Python程序中的实现。

import datetime 

d,m,y=map(int,input("Enter date: ").split())

try:
	s=datetime.date(y,m,d)
	print("Date is valid.")
except ValueError: 
	print("Date is invalid.")

Output

输出量

RUN 1:
Enter date: 10 10 2010
Date is valid.

RUN2:
Enter date: 30 2 2019
Date is invalid.

Explanation:

说明:

In the above program, we have imported the datetime module and taken the date in the form of d, m, y where d means day, m means month and y means year. Since some date input provided by the user may be valid or not that's why initially in the try block we are checking the date validation and if it is valid then print otherwise it will show ValueError. According to the try-except statement, the error found in try blocks is handled by except. So, we have except the ValueError and print the given date is invalid.

在上面的程序中,我们导入了datetime模块,并采用d , m , y的形式表示日期,其中d表示日, m表示月, y表示年。 由于用户提供的某些日期输入可能有效,所以这就是为什么最初在try块中我们要检查日期验证 ,如果有效,则打印,否则将显示ValueError。 根据try-except语句,在try块中发现的错误由except处理。 因此,除了ValueError之外,我们没有打印给定的日期。

翻译自: https://www.includehelp.com/python/check-the-given-Date-is-valid-or-not-in-python.aspx

python给定日期星期几

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值