MFC中 日期字符串的轉換


一、將字符串2011-08-1800:00:00轉換為字符串2011-8-18,通過以下的函數

CString DataDeleteZero(CString DATA)

{

         CStringstrmonth,strday,stryear;

         intyear=0,month=0,day=0;

         year=atoi(DATA.Mid(0,4));

         month=atoi(DATA.Mid(5,2));

         day=atoi(DATA.Mid(8,2));

         if(month<10)month=atoi(DATA.Mid(6,2));

         if(day<10)day=atoi(DATA.Mid(9,2));

         stryear.Format("%d",year);

         strmonth.Format("%d",month);

         strday.Format("%d",day);

         DATA=stryear+"-"+strmonth+"-"+strday;

         returnDATA;

}

二、MFC獲得日期控件時間的cstring格式

變量定義:
CString strDate;
COleDateTime ole_time;
CTime c_time;
1、CString轉換為COleDateTime

strDate = "2009-4-25 12:30:29";
ole_time.ParseDateTime(strDate);


2、COleDateTime轉換為CString
strDate = ole_time.Format("%Y-%m-%d %H:%M:%S");

3、COleDateTime轉換為CTime

SYSTEMTIME sys_time;
ole_time.GetAsSystemTime(sys_time);
c_time = CTime(sys_time);

4、CTime轉換為COleDateTime

SYSTEMTIME sys_time;
c_time.GetAsSystemTime(sys_time);
ole_time = COleDateTime(sys_time);

5、CTime轉換為CString

CTime Time;

Time.Format(“%Y-%m-%d”);

得到的是2011-08-18格式的字符串時間

 

Time.Format(“%y-%m-%d”);

得到的是11-08-18格式的字符串時間,年份和上面不一樣了

 

Time.Format(“%Y-%#m-%#d”);

得到的是2011-8-18格式的字符串時間,可以把月份與日的“0”去掉

 

Time.Format("%Y-%m-%d %H:%M:%S");

得到的是2011-8-18 00:00:00格式的字符串時間

 

 

來源:http://blog.csdn.net/fuyanzhi1234/article/details/6736241


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM