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

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值