程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C 日期增加 CString和CTime轉換

C 日期增加 CString和CTime轉換

編輯:C++入門知識

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());
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved