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

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

VC6

CString sTime = _T("2007-10-26 13:20:30"); 

char *charTime = (LPSTR)(LPCTSTR)sTime;

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

char *charFormat = (LPSTR)(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"));

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"));


发布了519 篇原创文章 · 获赞 97 · 访问量 182万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览