How do I check if the date entered is correct?

Asked 2 years ago, Updated 2 years ago, 38 views

I would like to check if the date exists from the method of entering the 4-digit date specified here.

For example, August 23rd is like 0823, and this is a date that actually exists, but if it is an impossible date like 9999, I would like to issue an error.
I would appreciate it if you could tell me how to check if a 4-digit number exists from the 4-digit and numeric check.

time=input("Please enter a date:")
iflen(time) == 4:
    if time.isdigit() == True:
        print("Normal")
    else:
        print("Not a number")
else:
    print("The number of digits is different"")

python python3

2022-09-30 19:34

4 Answers

If you google "python date validation", for example, the following pages will be hit.

How do I validate a date string format in python?- Stack Overflow

Refer to one of the answers, which shows you how to use the datetime module:

 from datetime import datetime

datetime.strptime(date_string, "%Y-%m-%d")

If an incorrect date is specified, a ValueError will occur, so you may want to write an exception using try and expect.

To apply to the example of this question, the formatting part of the date is as follows:

 datetime.strptime(date_string, "%m%d")


2022-09-30 19:34

The following is how to use the Pandas module to_datetime().

import pandas as pd

default_date_string(date_str):
    return len(date_str) == 4 and \
           pd.to_datetime(f'2000 {date_str}, format='%Y%m%d', errors='coerce')is not pd.NaT

if__name__=='__main__':
    testing=['0801', '0229', '9999', 'abcd' ]
    Fort in testing:
        print(validate_date_string(t))


2022-09-30 19:34

It's an irregular code, but you can also check the validity of the regular expression.
In the code below, passing a string to the is_date function returns True if it is a 4-digit month-day string (including leap year "0229").

import re

defis_date(s:str)->bool:
    pattern=re.compile("(01|03|05|07|08|10|12)(0[1-9]|[12][0-9]|3[01]$|"# Months through 31st\
                         "(04|06|09|11) (0[1-9]|[12][0-9]|30)$|"#Month through 30th\
                         "(02(0[1-9]|[12][0-9]))$")#29th month
    return pattern.match(s)!=None

# normal data
assert is_date("0101")
assert is_date("0229")
assert is_date("0430")
assert is_date("1231")

# anomaly data
assert is_date("0000") == False
assert is_date("0132") == False
assert is_date("0230") == False
assert is_date("1301") == False
assert is_date("9999") == False
assert is_date("101") == False
assert is_date("01011") == False
assert is_date("OIOI") == False

References

Read the regular expression to check the validity of the yyyymmdd format date string


2022-09-30 19:34

That's what I think.

  • Initialize/Define what the last days (days) of each month are from January to December first
  • Enable February 29th at any time because "year" is not specified
  • Replace the current print("Normal") with the following actions:
    • Cutting the entered string into an integer variable representing the month and day by two digits
    • If the month is 1 to 12 and the day is 1 or more (in the first defined list) and less than the last day of the month, the words "normal" and "not the month" are used to indicate abnormalities.
  • Cutting the entered string into an integer variable representing the month and day by two digits
  • If the month is 1 to 12 and the day is 1 or more (in the first defined list) and less than the last day of the month, the words "normal" and "not the month" are used to indicate abnormalities.


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.