Comparing 2 dates in c++

后端 未结 4 2113
独厮守ぢ
独厮守ぢ 2020-12-17 02:43

I was wondering if there is any relatively easy and short date comparison functions in C++. My dates are of type char*, and have the following format: DD

4条回答
  •  长情又很酷
    2020-12-17 03:08

    You need to extract the numeric data from the string. Worst case scenario is a bunch of loops and string to integer conversion functions.

    You can do it easily with sscanf and sprintf. If you're used to printf and scanf then this is simple to understand, and you can easily adapt it to other cases. There are no secret magic function calls.

    #include 
    void main()
    {
        char* date1 = "9\\12\\2012"; 
        char* date2 = "6\\11\\2013"; 
    
        int day1,month1,year1;
        int day2,month2,year2;
    
        sscanf(date1,"%d\\%d\\%d",&day1,&month1,&year1); //reads the numbers
        sscanf(date2,"%d\\%d\\%d",&day2,&month2,&year2); //from the string
    
        if (year1= date2\n");
        }
    
        char newdate[15];
    
        sprintf(newdate,"%d\\%d\\%d",13,2,1998); //make a date string from numbers
        printf("%s\n",newdate);
    }
    

提交回复
热议问题