碰到一個需求,將字符串"20121231"日轉換爲時間戳,很簡單,但是經常碰到,記錄一下:
// 年月日轉時間戳
long GetTime(string strEndDate)
{
string year = strEndDate.substr(0,4);
string mon = strEndDate.substr(4,2);
string day = strEndDate.substr(6,2);
tm local; //本地時間
local.tm_year = atoi(year.c_str()) -1900;
local.tm_mon = atoi(mon.c_str()) - 1;
local.tm_mday = atoi(day.c_str());
local.tm_hour = 0;
local.tm_min = 0;
local.tm_sec = 0;
long tmSec = mktime(&local);
cout << tmSec << endl;
return tmSec;
}