如何检查日期格式(日/月/年)是否正确C++

How to check if date format(dd/mm/yyyy) is correct in C++?

本文关键字:是否 C++ 格式 何检查 检查 日期      更新时间:2023-10-16

我制作了一个程序,可以对日期进行排序并按年份显示它们。 0+岁,10+岁,20+岁等。我也验证了日期。例如,不应超过 31 天或少于 1 天。我也有闰年、月份等的验证。如何检查日期格式是否有效?我想显示一条错误消息,指出输入日期为 6/7/2008 或 6/07/2008 时的日期格式不正确。格式应为 dd/mm/yyyy。

Person People::getPerson()
    {
        Person person;
        Validation validate;
        string input;
        bool ch;
        int memNo = 0;
        cout << " nEnter another Personn" << "Membership Number: ";
        cin >> memNo;
        person.SetMemNo(memNo);
        //While the input entered is not an integer, prompt the user to enter an integer.
        while(cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<int>::max(), 'n');

            cout << "Invalid data. Please type an integer! " << endl;
            cout << "Membership Number: ";
                cin >> memNo;
                cout << endl;


        }

        cout << "Enter first name of person: ";
        cin >> input;
        bool onlystring = validate.IsDigitsOnly(input);
                  //validate string for only alphabets,no                                                                           numbers
            if (onlystring == true) {
                cout << "Enter only alphabets for first name" << endl;
                cout << "Enter first name of person: ";
                cin.clear();
                cin.ignore();
                cin >> input;
            }
        person.SetFirstName(input);
        cout << "Enter your Surname ";
        cin >> input;
        person.SetLastName(input);
        bool onlystring1 = validate.IsDigitsOnly(input);          //validate string for only alphabets,no                                                                       numbers
            if (onlystring1 == true) {
                cout << "Enter only alphabets for last name" << endl;
                cout << "Enter your Surname ";
                cin >> input;
            }
            bool valid_date = false;
            do {
                cout << "Enter your date of joining(DD/MM/YYYY): ";
                cin >> input;

                string new_date= validate.checkFormat(input);
                valid_date = validate.valDate(new_date);
                if (valid_date == true)
                    person.SetDateJoined(new_date);
                else 
                    cout << "Invalid Date!Please  re-enter date of joining!" << endl;

            } while(valid_date == false);

日期验证

bool Validation::valDate(string input)
{
    int Y = stoi(input.substr(6));
    int M = stoi(input.substr(3, 2));
    int D = stoi(input.substr(0, 2));
    //check year
    if (Y >= 1900 && Y <= 9999)
    {
        //check month
        if (M >= 1 && M <= 12)
        {
            //check days
            if ((D >= 1 && D <= 31) && (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12)) {
                return true;
            }
            else if ((D >= 1 && D <= 30) && (M == 4 || M == 6 || M == 9 || M == 11)) {
                return true;
            }
            else if ((D >= 1 && D <= 28) && (M == 2)) {
                return true;
            }
            else if (D == 29 && M == 2 && (Y % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0))) {
                return true;
            }
            else {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        printf("Year is not valid.n");
        return false;
    }
    return 0;
}

int Validation::Prepend_function ( int n) {
    if (n < 10 && n < 0) {
        string num = to_string(n);
        string new_num = "0" + num;
        int number = stoi(new_num);
        return number;
    }
    else {
        return n;
    }
}

string Validation::checkFormat(string input)
{
    //check the length of the string
    int len = input.size();
    if (len != 10)
    {
        int year = stoi(input.substr(4));
        int month = stoi(input.substr(2, 1));
        int day = stoi(input.substr(0, 1));
        int prepend_day = Prepend_function(day);
        int prepend_month = Prepend_function(month);
        string day1 = to_string(prepend_day);
        string month1 = to_string(prepend_month);
        string year1 = to_string(year);
        string date = day1+"/"+month1+"/"+year1;
        return date;
    }
    }

我想你在问如何实现Validation::checkFormat,因为Validation::valDate似乎没有错。

std::string Validation::checkFormat(std::string input)
{
    std::stringstream in(input);
    int day, month, year;
    char ignore;
  // Match num/num/num, otherwise invalid
    if (in >> day >> ignore >> month >> ignore >> year)
    {
        std::stringstream out;
      // Force leading 0s
        out << std::setfill('0') << std::setw(2) << day << '/' << month << '/' <<  std::setw(4) << year;
        return out.str();
    }
  // have to return something if it isn't valid
    return std::string();
}

请注意,checkFormat听起来是返回以特定方式格式化的字符串的错误名称。我会称之为fixFormat,并将inputfixFormat的结果进行比较。如果它们相同,则input采用指定的格式