tags:

views:

8895

answers:

6

I know this may be simple but being C++ I doubt it will be. How do I convert a string in the form 01/01/2008 to a date so I can manipulate it? I am happy to break the string into the day month year constituents. Also happy if solution is windows only.

+6  A: 
#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *tm);
kenny
A: 

sscanf is what you need. Try this (this is for dd/mm/yyyy, perhaps you want mm/dd/yyyy).

#include <stdio.h>

int main() {
  char test[] = "01/01/2008";

  int day, month, year;

  sscanf(test, "%d/%d/%d", &day, &month, &year);
  printf("day: %d\n",day);
  printf("moth: %d\n",month);
  printf("year: %d\n",year);
}
schnaader
printf()/scanf() in C++? Shame! :)
e.James
A: 

strptime doesn't seem to be available (am using mingw compiler). Are there any other options?

Ubervan
+3  A: 

You could try Boost.Date_Time Input/Output.

Ferruccio
Dates are not simple, just look at the US getting it wrong. Boost::Date_Time may look like big, but it's just a small first step.
MSalters
+2  A: 

I figured it out without using strptime: Break the date down into its components i.e. day month year then:

struct tm  tm;
time_t rawtime;
time ( &rawtime );
tm = *localtime ( &rawtime );
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
mktime(&tm);

tm can now be converted to a time_t and be manipulated.

Ubervan
A: 

The POCO library has a DateTimeParser class which might help you with that. http://www.appinf.com/docs/poco/Poco.DateTimeParser.html

Adrian Kosmaczewski