MFC中 日期字符串的转换

it2022-05-09  38

一、将字符串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转换为COleDateTimestrDate = "2009-4-25 12:30:29";ole_time.ParseDateTime(strDate);

2、COleDateTime转换为CStringstrDate = ole_time.Format("%Y-%m-%d %H:%M:%S");3、COleDateTime转换为CTimeSYSTEMTIME sys_time;ole_time.GetAsSystemTime(sys_time);c_time = CTime(sys_time);4、CTime转换为COleDateTimeSYSTEMTIME 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

转载于:https://www.cnblogs.com/lxt287994374/p/3367453.html

相关资源:数据结构—成绩单生成器

最新回复(0)