打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++日期时间与字符串间的转换 — Windows Live
 
2月21日

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
用CTime类得到当前日期、时间、星期,并格式化输出
COleDateTime和CTime类型的应用
如何在网页中显示服务器时间
VS2010/MFC编程入门之四十三(MFC常用类:CTime类和CTimeSpan类)
校验身份证号码有效性的代码
C 的多态性(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服