我知道这可能很简单,但作为C++,我对此表示怀疑。如何将格式为01/01/2008的字符串转换为日期,以便对其进行操作?我很高兴将字符串分解为日、月、年的组成部分。如果解决方案是Windows,我也很高兴。
发布于 2008-11-21 10:46:57
#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *tm);
发布于 2008-11-21 14:24:09
我没有使用strptime
就解决了这个问题。
将日期分解为其组成部分,即日、月、年,然后:
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
转换为time_t
并对其进行操作。
发布于 2014-11-04 23:45:03
#include <time.h>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char str[256];
cout << "Inter date: " << endl;
cin.getline(str,sizeof(str));
replace( str, str+strlen(str), '/', ' ' );
istringstream( str ) >> day >> month >> year;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
mktime ( timeinfo );
strftime ( str, sizeof(str), "%A", timeinfo );
cout << str << endl;
system("pause");
return 0;
}
https://stackoverflow.com/questions/308390
复制相似问题