C++日期时间与字符串间的转换(转载)

 

VC2005

 

不含中文格式

 

stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec); 

 

CString sTime = _T("2007-10-26 12:40:10"); 

 

TCHAR *charTime = (TCHAR*)(LPCTSTR)sTime;

 

CString sFormat = _T("%d-%d-%d %d:%d:%d");

 

TCHAR *charFormat = (TCHAR*)(LPCTSTR)sFormat;

 

int nYear;

 

int nMonth;

 

int nDate;

 

int nHour;

 

int nMin;

 

int nSec; 

 

_stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec);

 

CTime t(nYear,nMonth,nDate,nHour,nMin,nSec);

 

CString s = t.Format(_T("%Y-%m-%d %H:%M:%S"));

 

含中文格式

 

要包含#include <locale.h>

 

CString sTime = _T("2007-10-26 12:40:10"); 

 

TCHAR *charTime = (TCHAR*)(LPCTSTR)sTime;

 

CString sFormat = _T("%d-%d-%d %d:%d:%d");

 

TCHAR *charFormat = (TCHAR*)(LPCTSTR)sFormat;

 

int nYear;

 

int nMonth;

 

int nDate;

 

int nHour;

 

int nMin;

 

int nSec; 

 

_stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec);  

 

CTime t(nYear,nMonth,nDate,nHour,nMin,nSec);

 

_tsetlocale(LC_ALL,_T("Chinese-simplified")); //本地化

 

CString s = t.Format(_T("%Y%m%d%H%M%S"));

 

posted @ 2013-11-01 16:46  吞天小狗  阅读(254)  评论(0)    收藏  举报