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_tBeginDate2COleDataTime
//一個爲起始時間,一個爲終止時間
//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;
   }
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章