Python 没有 input/invalid 输入的日期验证

Python date validation with no input/invalid input

该计划的目标是:

  1. 验证输入格式,日期范围不能超过2个月
  2. 无input/invalid输入->return当前月的上半月或下半月基于当月的日期
  3. 上半年是从第 1 天到第 15 天,下半年是从第 16 天到月底

第一个代码片段的硬代码按预期工作并输出正确,但是第二个代码片段中的日期验证不会跳转到 except 块中指示的方法,这给我带来了问题并且没有显示正确的输出。

import os, sys
import datetime as dt

if __name__ == '__main__':
    validate_range('2003/12/23', '2003/12/22')
    validate_range('2003/10/23', '2003/11/22')
    validate_range('2003/12/23', '2003/10/22')
    validate_range('2003/7/23', '2003/10/22')

输出正确的代码片段(不检查日期格式是否正确):

def validate_range(first_date, second_date):
    start_date = dt.datetime.strptime(first_date, '%Y/%m/%d')  # Get first date
    end_date = dt.datetime.strptime(second_date, '%Y/%m/%d')  # Get second date

    num_months = (end_date.year - start_date.year) * 12 + (
            end_date.month - start_date.month)  # Get year/month within 2 months

    if num_months >= 0 and num_months <= 2:
        print("In Range")
    else:
        current_day = dt.datetime.today().day

        if current_day >= 1 and current_day <= 15:
            print("First half")
        if current_day >= 16 and current_day <= 32:
            print("Second half")

正确输出:

In Range
In Range
First half
First half

未显示正确输出的代码片段(带日期验证):

def read_args(first_date, second_date):
    try:
        start_date = dt.datetime.strptime(first_date, '%Y/%m/%d')  # Get first date
        end_date = dt.datetime.strptime(second_date, '%Y/%m/%d')  # Get second date
        v_range(start_date, end_date)
    except:
        w_range()


def v_range(first_date, second_date):
    num_months = (second_date.year - first_date.year) * 12 + (
            second_date.month - first_date.month)  # Get year/month within 2 months
    if num_months >= 0 and num_months <= 2:
        print("In Range")


def w_range():
    current_day = dt.datetime.today().day

    if current_day >= 1 and current_day <= 15:
        print("First half")
    if current_day >= 16 and current_day <= 32:
        print("Second half")

输出:

In Range
In Range

与您对 try / except 的使用保持一致,可能需要一些改进,但此示例证明这是可行的:

import os, sys
import datetime as dt


def read_args(first_date, second_date):
    try:
        start_date = dt.datetime.strptime(first_date, '%Y/%m/%d')  # Get first date
        end_date = dt.datetime.strptime(second_date, '%Y/%m/%d')  # Get second date
        result = v_range(start_date, end_date)
    except:
        pass
        # w_range()


def v_range(first_date, second_date):
    num_months = (second_date.year - first_date.year) * 12 + (
            second_date.month - first_date.month)  # Get year/month within 2 months
    if num_months >= 0 and num_months <= 2:
        print("In Range")
    else:
        w_range()


def w_range():
    current_day = dt.datetime.today().day

    if current_day >= 1 and current_day <= 15:
        print("First half")
    if current_day >= 16 and current_day <= 32:
        print("Second half")


if __name__ == '__main__':
    read_args('2003/12/23', '2003/12/22')
    read_args('2003/10/23', '2003/11/22')
    read_args('2003/12/23', '2003/10/22')
    read_args('2003/7/23', '2003/10/22')