VC笔记之时间

 
获得当前日期和时间
 
CTime tm=CTime::GetCurrentTime();
 CString str=tm.Format("%Y-%m-%d");
 
在VC中,我们可以借助CTime时间类,获取系统当前日期,具体使用方法如下:
 
CTime t = CTime::GetCurrentTime(); //获取系统日期
 
int d=t.GetDay(); //获得几号
 
int y=t.GetYear(); //获取年份
 
int m=t.GetMonth(); //获取当前月份
 
int h=t.GetHour(); //获取当前为几时
 
int mm=t.GetMinute(); //获取分钟
 
int s=t.GetSecond(); //获取秒
 
int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六
 
如果想计算两段时间的差值,可以使用 CTimeSpan 类,具体使用方法如下:
 
CTime t1( 1999, 3, 19, 22, 15, 0 );
 
CTime t = CTime::GetCurrentTime();
 
CTimeSpan span=t-t1; // 计算当前系统时间与时间t1 的间隔
 
int iDay=span.GetDays(); // 获取这段时间间隔共有多少天
 
int iHour=span.GetTotalHours(); // 获取总共有多少小时
 
int iMin=span.GetTotalMinutes();// 获取总共有多少分钟
 
int iSec=span.GetTotalSeconds();// 获取总共有多少秒
 
 
设置计时器  
 
定义TIMER ID
 
#define TIMERID_JISUANFANGSHI 2
 
在适当的地方设置时钟,需要开始其作用的地方;
 
SetTimer(TIMERID_JISUANFANGSHI,200,NULL);
 
在不需要的时候销毁掉时钟
 
 KillTimer(TIMERID_JISUANFANGSHI);
 
消息映射
 
void CJisuan::OnTimer(UINT nIDEvent)
{
   //定时处理的问题信息
}
 
 
 
CString 转换成 COleDateTime
strCString="2003-10-27 6:24:37"; //
COleVariant vtime(strCString);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;
 
 
 
COleDataTime 转换成 CTime
COleDateTime time1(1977,4,16,2,2,2); // SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);
 
 
 
 
time_t time2=tm.GetTime(); //CTime--->time_t
COleDateTime time3(time2); //time_t--->COleDateTime
 
 
 
COleDateTime double
COleDateTime    Time(2000,3,17,14,0,0);  
 double    a=(double)Time;  
 return;   
 
 
直接转换就是了   
          COleDateTime   dt;  
          ...  
          double   x=(double)dt.m_dt;  
   
 
 The   DATE   type   is   implemented   using   an   8-byte   floating-point   number.   Days   are   represented   by   whole   number   increments   starting   with   30   December   1899,   midnight   as   time   zero.   Hour   values   are   expressed   as   the   absolute   value   of   the   fractional   part   of   the   number.
 
       int year = tmm.GetYear();
       int month = tmm.GetMonth();
       int day = tmm.GetDay();
       int hour = tmm.GetHour();
       int minute=tmm.GetMinute();
       int second = tmm.GetSecond();
 
       CString m_lGetProDay;
       m_lGetProDay.Format("%04u-%02u-%02u %02u:%02u:%02u/n",year,month,day,hour,minute,second);
       COleVariant vsettime(m_lGetProDay);
       vsettime.ChangeType(VT_DATE);
       COleDateTime dtime=vsettime;
       return dtime;
 
 
日期相隔计算和判断
// m_tBeginDate1, m_tBeginDate2 COleDataTime
// 一个为起始时间,一个为终止时间
//COleDataTime--->CTime
 
SYSTEMTIME systime;
    VariantTimeToSystemTime(m_tBeginDate1, &systime);
    CTime tm(systime);
 
     SYSTEMTIME systime2;
        VariantTimeToSystemTime(m_tBeginDate2,&systime2);
        CTime tm1(systime2);
 
       if(tm > tm1)  
       {
              MessageBox("起始时间不能大于终止时间!","提示",MB_OK);
              return;
       }
//***********************************************************
// CTimeSpan的用法
/* MSDN介绍如下
CTimeSpan Operators
Operators
Assigns new time-span values.
Adds CTimeSpan objects.
Subtracts CTimeSpan objects.
Adds a CTimeSpan object to this CTimeSpan.
Subtracts a CTimeSpan object from this CTimeSpan.
Compares two relative time values.
 
Archive/Dump
Outputs a CTimeSpan object to CArchive or CDumpContext.
Inputs a CTimeSpan object from CArchive
 
CTimeSpan Member Functions
Construction
Constructs CTimeSpan objects in various ways.
 
Extraction
Returns the number of complete days in this CTimeSpan.
Returns the number of hours in the current day (–23 through 23).
Returns the total number of complete hours in this CTimeSpan.
Returns the number of minutes in the current hour (–59 through 59).
Returns the total number of complete minutes in this CTimeSpan.
Returns the number of seconds in the current minute (–59 through 59).
Returns the total number of complete seconds in this CTimeSpan.
 
Conversion
Converts a CTimeSpan into a formatted string.
时间的间隔处理
*/
       CTimeSpan m_timespan = tm1 - tm;
    //int iss=m_timespan.GetDays();
       if(m_timespan.GetDays() >30)
   {
        MessageBox("查询日期不能大于1个月","提示",MB_OK);
        return;
   }
 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值