MFC中CString、char*、int、COleDateTime之间的转换

本文介绍如何在C++中使用COleDateTime进行日期时间格式的转换,并演示了不同数据类型如CString与COleDateTime之间的转换方法。此外,还展示了如何将整数和CString转换为其他常用的数据格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、
COleDateTime转换到CString

    CString strDateTime = _T("2011-09-28 14:51:18.640"); 

    //除去豪秒
    int nIndex = strDateTime.ReverseFind('.');
    strDateTime = strDateTime.Left(nIndex);

    //转化为标准时间
    COleDateTime oleDate;
    oleDate.ParseDateTime(strDateTime);


    CString strDate = oleDate.Format(_T("%Y-%m-%d %H:%M:%S")); //格式为年-月-日- 时:分:秒
    CString strTime = oleDate.Format(_T("%H:%M:%S"));//格式为年月日
    int nDayOfWeek = oleDate.GetDayOfWeek();//值为1-7,对应周一到周日

2、 CString-转换到COleDateTime

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

3、int转换到CString

int a = 10;
CString str;

str.Format(_T("%d"), a ); 

AfxMessageBox(t);

4、CString 转化成 char*;

/*LPCTSTR 操作符(或者更明确地说就是 TCHAR * 操作符)在 CString 类中被重载了, 
该操作符的定义是返回缓冲区的地址,因此,如果你需要一个指向 CString 的 字符串指针的话, 
可以这样做:*/
CString s("GrayCat");
LPCTSTR p = s;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值