简体   繁体   English

C++ 中的日期验证和转换

[英]Date Validation and Conversion in C++

I have to write 2 functions.我必须写2个函数。 One that takes in a date as a string and checks if its in mm/dd/yy format;将日期作为字符串并检查其是否为 mm/dd/yy 格式的一种; if its not in the correct format, it should be edited to make it so.如果它的格式不正确,则应对其进行编辑以使其正确。 The other function should convert the validated date to the format "Month dd, 20yy".另一个函数应该将验证日期转换为“Month dd, 20yy”格式。

I'm pretty sure I can take care of the second function, but I am having trouble with the first one.我很确定我可以处理第二个功能,但是我在处理第一个功能时遇到了麻烦。 I just have no idea how to check if its in that format... any ideas?我只是不知道如何检查它是否是那种格式……有什么想法吗?

I thought that this would work, but it doesn't seem to...我以为这会奏效,但似乎没有……

Updated code:更新代码:

bool dateValidation(string shipDate)
{
    string temp;
    if(shipDate.length() == 8 )
    {
        if(shipDate[2] == '/' && shipDate[5] =='/')
        {
            int tempDay, tempMonth, tempYear;
            //Gather month
            temp = shipDate[0];
            temp += shipDate[1];
            //convert string to int
            tempMonth = temp.atoi;
            temp = "";

            //Gather day
            temp = shipDate[3];
            temp += shipDate[4];
            //convert string to int
            tempDay = temp.atoi;
            temp = "";

            //Gather year
            temp = shipDate[6];
            temp += shipDate[7];
            //convert string to int
            tempYear = temp.atoi;
            temp = "";

            if(tempMonth > 0 && tempMonth <= 12)
            {

                if(tempMonth == 9 ||
                   tempMonth == 4 ||
                   tempMonth == 6 ||
                   tempMonth == 11 ||)
                {
                    if(tempDay > 0 && tempDay <= 30)
                    {
                        if 30 days
                            }
                }
                else if(tempMonth == 2)
                {
                    if(tempDay > 0 && tempDay <= 28)
                    {
                        if 28 days
                            }
                }
                else
                {
                    if(tempDay > 0 && tempDay <= 31)
                    {
                        if 31 days
                            }
                }
            }
        }
    }
}

There are 4 things you want to check:您需要检查 4 件事:

  • Is there 8 characters ?有8个字符吗? If not, then don't even bother checking anything else.如果没有,那么甚至不要费心检查其他任何东西。 It's not in the proper format.它的格式不正确。
  • Are the third and fifth characters '/'.是第三个和第五个字符'/'。 If not, then you still don't have the proper format.如果没有,那么您仍然没有正确的格式。
  • Check each pair for its valid values.检查每对的有效值。 A month has days between 1 and 31 at most, there are no more than 12 months and months range from 01 to 12. A year can be any combination of any 2 digits.一个月最多有 1 到 31 天,不超过 12 个月,月份从 01 到 12。年份可以是任意 2 位数字的任意组合。

This should take care of the format, but if you want to make sure that the date is valid:这应该注意格式,但如果您想确保日期有效:

  • Check for valid number of days in each month (january 31, february 28-29...) and indeed check for those leap years.检查每个月(1 月 31 日、2 月 28-29 日...)的有效天数,并确实检查那些闰年。

This looks a lot like a project I am about to grade.... You should verify that it is Gregorian Calendar compliant if it is the project I am about to grade.这看起来很像我要评分的项目......如果它是我要评分的项目,您应该验证它是否符合公历。 1/1/2012 is definitely valid though so what you may want to do and what I would hope you consider is creating a switch statement that examines for formats like 1/12/2012 and 10/2/2012 because these are valid. 2012 年 1 月 1 日绝对有效,因此您可能想要做的以及我希望您考虑的是创建一个 switch 语句,用于检查 1/12/2012 和 10/2/2012 等格式,因为它们是有效的。 Then parse out the month day and year from these.然后从这些中解析出月日和年。 Then verify that they are within the limit of the Gregorian calendar.然后验证它们是否在公历的范围内。 If it is for a class which I would guess that it is, you should consider writing the verification as a separate function from the parsing function.如果是针对我猜是这样的类,您应该考虑将验证编写为与解析函数分开的函数。

So first ask whether the date is too long if not, is it too short, if not which version is it, then pass the dmy to the verification function.所以先问日期是不是太长了,是不是太短了,不是是哪个版本,然后把dmy传给验证函数。 This kind of modularity will simplify your code and reduce instructions.这种模块化将简化您的代码并减少指令。

something like就像是

bool dateValidation(string shipDate) { string temp; bool dateValidation(string shipDate) { string temp;

switch(shipDate.length())
{
     case(10):
        // do what  your doing
        verify(m,d,y);
        break;

     case(8):
        //dealing with single digits
        // verify 1 and 3 are '/' and the rest are numbers
        verifiy(m,d,y);
        break;

     case(9):
        //a little more heavy lifting here 
        // but its good thinking for a new programmer
        verifiy(m,d,y);
        break;
     default:       

      //fail message
        break;
}

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

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