当前位置:编程学习 > C/C++ >>

C 日期增加 CString和CTime转换

CString s("2001-8-31");

int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t1(nYear, nMonth, nDate, nHour, nMin, nSec);

CTimeSpan ts(1,0,0,0);//一天   增加两天(2,0,0,0)
t1+= ts;//t1 = 2001-9-1
CString strDay;
strDay.Format("%d-%d-%d",t1.GetYear(),t1.GetMonth(),t1.GetDay() );
AfxMessageBox(strDay);//2001-9-1

 


CString s("2001-8-31");
CString s1(" 00:00:00");
s = s+s1;
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t1(nYear, nMonth, nDate, nHour, nMin, nSec);
CTimeSpan ts(1,0,0,0);//一天
t1+= ts;//t1 = 2001-9-1
CString strDay;
strDay.Format("%d-%d-%d",t1.GetYear(),t1.GetMonth(),t1.GetDay() );
s = strDay;
AfxMessageBox(s);//2001-9-1

 

//下面的可以把天数和月份小与10的前面加0      比如2001-9-8 ------->2001-09-08

if(t1.GetMonth() < 10 && t1.GetDay() < 10)
{
 strDay.Format("%d-0%d-0%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}
else if(t1.GetMonth() < 10)
{
 strDay.Format("%d-0%d-%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}
else
{
 strDay.Format("%d-%d-0%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,