MFC日期字符串转换

一、将字符串2011-08-1800:00:00转换为字符串2011-8-18,通过以下的函数

CString DataDeleteZero(CString DATA)

{

  CString strmonth,strday,stryear;

  int year=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格式的字符串时间

原文地址:https://www.cnblogs.com/JackieWu/p/4665085.html