// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <tchar.h>
/**
*@biref 获取时间
*@param 2013-11-11 18:54:12 format:_T("%d-%d-%d %d:%d:%d")
*/
double getTimeByStr(std::wstring timeStr, std::wstring format)
{
struct tm _tm;
int year, month, day, hour, minite, second;
_stscanf_s(timeStr.c_str(), format.c_str(), &year, &month, &day, &hour, &minite, &second);
_tm.tm_year = year;
_tm.tm_mon = month;
_tm.tm_mday = day;
_tm.tm_hour = hour;
_tm.tm_min = minite;
_tm.tm_sec = second;
_tm.tm_isdst = -1;
//s
time_t tt = mktime(&_tm);
//ms
return tt * 1000;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring format = _T("%d-%d-%d %d:%d:%d");
getTimeByStr(_T("2013-11-11 18:54:12"), format);
return 0;
}
参考内容
1,已经确定了字符串格式,获取格式中的值
http://msdn.microsoft.com/en-us/library/zkx076cy.aspx
http://blog.csdn.net/jonathandj/article/details/4847592
2,mktime