CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

送分题(100分):如何将一个CString转成CTime?

楼主Lipli()2003-06-04 10:52:25 在 VC/MFC / 基础类 提问

CString   str;  
  str="2003-6-4";  
  CTime   t;  
  ???请教:怎样将str赋给t呢? 问题点数:100、回复次数:6Top

1 楼ccwing(不是背书郎)回复于 2003-06-04 11:15:20 得分 10

为什么计算一下字符串。。。你这样认为简单了是不???  
   
  OK,下面有用牛刀杀鸡的办法。。。呵呵  
   
  首先  
  #include   <comdef.h>  
   
                    CString   str;  
  _bstr_t   bstr;  
  _variant_t   vt;  
  COleDateTime   olet;  
   
   
  str="2003-6-4";  
  bstr=str;  
  vt=bstr;  
  olet=vt;  
   
  CTime   t(olet.GetYear(),olet.GetMonth(),olet.GetDay(),0,0,0);Top

2 楼greenever(司马青杉)回复于 2003-06-04 11:16:18 得分 30

笨办法。  
  把str中的年月日分别读出到三个字符串中,然后用atoi转换成整数,再用CTime的构造函数就行了CTime(Year,Month,Day,0,0,0)  
   
  算了,随手写一个给你:  
  CString   strYear   =   str.Left(4);  
  CString   strMonth   =   str.Mid(5,1);  
  CString   strDay   =   str.Right(1);  
  /*注意,如果月、日的数字是两位数,就不行了。所以没有通用性。为了有通用性你可以先对字符串作处理,把月、日都加0补齐成两位数。*/  
  int   nYear   =   atoi(strYear);  
  int   nMonth   =   atoi(strMonth);  
  int   nDay   =   atoi(strDay);  
  CTime   t   =   CTime(nYear,nMonth,nDay,0,0,0);Top

3 楼pinghell(笑春风)回复于 2003-06-04 11:27:21 得分 20

int   nYear,nMonth,nDay,nHour,nMin,nMiao;  
  nYear=?;  
  ……  
  Ctime   m_time=(nYear,nMonth,nDay,nHour,nMin,nMiao);Top

4 楼triggerd(有点着急)回复于 2003-06-04 11:56:01 得分 30

CString   s="2003-06-4";  
  CString   date[3];  
  date[0]=s.Mid(0,4);  
  date[1]=s.Mid(5,2);  
  date[2]=s.Mid(8,2);  
  int   date2[3];  
  for(int   i=0;i<3;i++)  
        date2[i]=atoi(date[i]);  
  CTime   time(date2[0],date2[1],date2[2],0,0,0);  
  Top

5 楼free_card(痛并快乐着)回复于 2003-06-04 12:12:21 得分 10

BOOL   ScanTime  
  (  
  Ctime   &time,   //   o   -   filled   in   time   structure  
  LPCTSTR   lpszTime,   //   I   -   the   string   containing   the   time   to  
  be   extracted  
  LPCTSTR   lpszFormat   //   I   -   the   time   format   to   extract  
  according   to  
  )  
  {  
  int   nYear   =   1980;   //   extracted   time   fields  
  int   nMonth   =   1;  
  int   nDay   =   1;  
  int   nHour   =   0;  
  int   nMin   =   0;  
  int   nSec   =   0;  
  int   nFlag   =   DATE_TIME;   //   DATE_TIME   /   DATE_ONLY   /   TIME_ONLY  
  Cstring   msg;//   start   at   the   beginning  
  char   *pTime   =   (char   *)lpszTime;  
  char   *pFmt   =   (char   *)lpszFormat;  
  while   (*pFmt   !=   '\0')  
  {  
  if   (*pFmt   ==   '%')  
  {  
  pFmt++;  
  switch   (*pFmt)  
  {  
  case   'Y'   :   //   year   with   century  
  sscanf   (pTime,"%4d",&nYear);  
  if   (nYear   <1980   ||   nYear>   2036)  
  {  
  msg.Format   ("Invalid   year   (%d)",nYear);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=4;  
  break;  
  case   'y'   :   //   year   without   century   (00-99)  
  sscanf   (pTime,"%2d",&nYear);  
  nYear   =   nYear   +   (nYear   >   36   ?   1900   :   2000);  
  if   (nYear   <1980   ||   nYear>   2036)  
  {  
  msg.Format   ("Invalid   year   (%d)",nYear);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  case   'm'   :   //   month   (01-12)  
  sscanf   (pTime,"%2d",&nMonth);  
  if   (nMonth   <1   ||   nMonth>   12)  
  {  
  msg.Format   ("Invalid   month   (%d)",nMonth);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  case   'd'   :   //   day   of   month   (01-31)  
  sscanf   (pTime,"%2d",&nDay);  
  if   (nDay   <1   ||   nDay>   31)  
  {  
  msg.Format   ("Invalid   day   (%d)",nDay);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  case   'H'   :   //   hour   (00-23)  
  sscanf   (pTime,"%2d",&nHour);  
  if   (nHour   <0   ||   nHour>   23)  
  {  
  msg.Format   ("Invalid   hour   (%d)",nHour);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  case   'M'   :   //   minute   (00-59)  
  sscanf   (pTime,"%2d",&nMin);  
  if   (nMin   <0   ||   nMin>   59)  
  {  
  msg.Format   ("Invalid   minute   (%d)",nMin);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  case   'S'   :   //   second   (00-59)  
  sscanf   (pTime,"%2d",&nSec);  
  if   (nSec   <1   ||   nSec>   31)  
  {  
  msg.Format   ("Invalid   second   (%d)",nSec);  
  AfxMessageBox   (msg);  
  return   (FALSE);  
  }  
  pTime+=2;  
  break;  
  default   :  
  msg.Format("Invalid   format   specifier   (%c)",*pFmt);  
  AfxMessageBox(msg);  
  return   (FALSE);  
  break;  
  }  
  }  
  else  
  {  
  if   (!isdigit((int)(*pTime)))  
  {  
  if   (*pTime   ==   *pFmt)  
  {  
  pTime++;  
  }  
  else  
  {  
  msg.Format   ("Character   mismatch   :   Expected   %c,   found  
  %c",*pFmt,*pTime);  
  AfxMessageBox   ((LPCTSTR)msg);  
  return   (FALSE);  
  }  
  }  
  }  
  pFmt++;  
  }  
  //   success  
  time   =   Ctime(nYear,nMonth,nDay,nHour,nMin,nSec);  
  return   (TRUE);  
  }  
   
   
  Top

6 楼Lipli()回复于 2003-06-04 13:36:46 得分 0

好~~好~~~好~~~给分~~~~Top

相关问题

  • 遇到Double转成CString的问题
  • =====怎样把DWORD转成CString?======
  • CString 转成 int类型?
  • 如何将CString转成char*
  • 如何将CString转成char*
  • 如何把float转成CString
  • 怎么把 CSTRING 转成BSTR
  • CString 怎样转成Int????????
  • 简单问题:如何把CString类型存放的数字转成int型?
  • 超级简单问题:如何把char[20]类型转成CString类型?

关键词

  • date
  • ptime
  • nyear
  • ctime
  • nmonth
  • nday
  • olet
  • nhour
  • pfmt
  • afxmessagebox

得分解答快速导航

  • 帖主:Lipli
  • ccwing
  • greenever
  • pinghell
  • triggerd
  • free_card

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo