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
2008-11-21 10:46:56
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
2008-11-21 10:47:55
printf()/scanf() in C++? Shame! :)
e.James
2008-11-21 10:51:28
A:
strptime doesn't seem to be available (am using mingw compiler). Are there any other options?
Ubervan
2008-11-21 11:46:15
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
2008-11-21 13:03:48
+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
2008-11-21 14:24:08
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
2010-01-13 16:36:41