2

I need to compare 2 string dates to see if one date is later then another. The date format for both dates is at the bottom. i can rearrange this for what ever is easiest. I have boost but it doesn't have to be, ive been through so many examples and can't seem to wrap my brain around getting it to work. Thanks in advance basically i want

2012-12-06 14:28:51

if (date1 < date2) {
 // do this
}
else {
 // do that
}  
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
user1054513
  • 605
  • 3
  • 13
  • 24

3 Answers3

7

It looks like the date format your using is already in lexicographical order and a standard string comparison will work, something like:

std::string date1 = "2012-12-06 14:28:51";
std::string date2 = "2012-12-06 14:28:52";
if (date1 < date2) {
    // ...
}
else {
    // ...
}

You will need to make sure that spacing and punctuation is consistent when using this format, in particular something like 2012-12-06 9:28:51 will break the comparison. 2012-12-06 09:28:51 will work though.

DRH
  • 7,868
  • 35
  • 42
  • Thanks didn't know it would be that simple – user1054513 Dec 06 '11 at 21:12
  • to me it just makes a lot more sense in general for prog languages to convert dates to an integer and then compare integers; comparing things "lexicographically" is just error-prone on the part of a developer. – Alexander Mills Jan 11 '17 at 21:04
4

You're lucky - your dates are already in the proper format to do a standard string comparison and get the correct results. All the parts go from most significant to least significant, and you're using 24-hour times.

If these are std::strings you can use the < just as you have in your sample. If they're C-style character array strings, use strcmp.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    As long as they're UTC. Or, more generally, the same timezone. – smparkes Dec 06 '11 at 21:02
  • 1
    You would be right, if and only if the hours lesser than 10 are zero padded, eg. 01:00:00 < 02:00:00, but 1:00:00 > 02:00:00. @Seth Carnegie Is that really the case? – fronsacqc Dec 06 '11 at 21:02
0

strcmp() returns an integral value indicating the relationship between the strings:

 result = strcmp( string1, string2 ); 
 if( result > 0 )  strcpy( tmp, "greater than" );
 else if( result < 0 )  strcpy( tmp, "less than" );

A zero value indicates that both strings are equal. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

#include <string.h>
#include <stdio.h>

char string1[] = "2012-12-06 14:28:51";
char string2[] = "2011-12-06 14:28:51";

int main( void )
{
   char tmp[20];
   int result;

   printf( "Compare strings:\n   %s\n   %s\n\n\n", string1, string2 );
   result = strcmp( string1, string2 );

   if( result > 0 )        strcpy( tmp, "greater than" );
   else if( result < 0 )   strcpy( tmp, "less than" );
   else    strcpy( tmp, "equal to" );

   printf( "   strcmp:   String 1 is %s string 2\n\n", tmp );

   return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28