请问vc如何计算两个日期之间差多少天?

解决方案 »

  1.   

    如果是OLE类型的时间,做减法取整就可以
      

  2.   

    CTime a,b;
    CTimeSpan c;c = a - b;
      

  3.   

    //  用这个好点COleDateTime 
    COleDateTime t1;
    COleDateTime t2;
    COleDateTimeSpan t12 = t1 - t2;
    int nDay = t12.GetDays();
      

  4.   

    t1,t2怎么获取当时时间呢?getcurrenttime???
      

  5.   

    COleDateTime::SetDateTimeint SetDateTime( int nYear, int nMonth, int nDay, int nHour, int nMin, intnSec );返回值:
    如果成功设置该COleDateTime对象的值,则返回0;否则返回1。该返回值基于DateTimeStatus枚举类型。要了解更多的信息,请参阅SetStatus成员函数。参数: nYear, nMonth, nDay, nHour, nMin, nSec 指示复制到该COleDateTime对象的日期和时间部分。 
      

  6.   

    COleDateTime::GetCurrentTimestatic COleDateTime PASCAL GetCurrentTime( );说明:
    调用该静态成员函数返回当前日期/时间值。
      

  7.   

    以前由于项目需求,写过这样一个函数:
    /********************************************************************
    *  Function: SubTime      
    *  Description: 计算两个日期天数之差   
    *  Input: CString strStart [开始时间,如2010-05-28] 
    * CString strEnd [结束时间,如2010-06-28] 
    *  Output: None
    *  Return: CString [天数差]     
    *  Others:      None   
    *********************************************************************/
    CString SubTime(CString strStart,CString strEnd)
    {
    CString  strDates = "0";
    if(strStart.IsEmpty() || strEnd.IsEmpty())
    {
    return strDates;
    }

    COleVariant  vtime1(strStart);
    COleVariant  vtime2(strEnd);
    vtime1.ChangeType(VT_DATE);
    vtime2.ChangeType(VT_DATE);

    COleDateTime timeS = vtime1;
    COleDateTime timeE = vtime2;
    SYSTEMTIME  stS,stE;
    VariantTimeToSystemTime(timeS,&stS);
    VariantTimeToSystemTime(timeE,&stE);

    CTime tmS(stS);
    CTime tmE(stE);
    CTimeSpan span = tmE-tmS;
    strDates.Format("%d",span.GetDays());
    return strDates;
    }
      

  8.   

    For example:
    void main()
    {
        MessageBox(NULL,SubTime("2010-02-01","2010-03-01"),NULL,NULL);
    }
      

  9.   

    msdn:CTime::operator +, -
    CTime operator +( CTimeSpan timeSpan ) const;
    CTime operator -( CTimeSpan timeSpan ) const;
    CTimeSpan operator -( CTime time ) const;
    Res
    CTime objects represent absolute time. CTimeSpan objects represent relative time. The first two operators allow you to add and subtract CTimeSpan objects to and from CTime objects. The third allows you to subtract one CTime object from another to yield a CTimeSpan object.
    Example// example for CTime::operator  +, -
    CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
    CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999
    CTimeSpan ts = t2 - t1;  // Subtract 2 CTimes
    //然后再
     ts.GetDays();
      

  10.   

    COleDateTime t1(1999, 1, 1, 12, 13, 14);
    COleDateTime t2 = COleDateTime::GetCurrentTime();
    COleDateTime t3;
    t3.ParseDateTime("2000-01-13 11:12:13");
      

  11.   


    #include <windows.h>
    #include <ATLComTime.h>
    void main()
    {
    SYSTEMTIME st1, st2; //GetLocalTime(&st1);              // gets current time
    //GetLocalTime(&st2); //手动设置时间
    st1.wYear = 2010; //依次 年月日时分秒
    st1.wMonth = 9;
    st1.wDay = 24;
    st1.wHour = 0;
    st1.wMinute = 0;
    st1.wSecond = 0; st2.wYear = 2010;
    st2.wMonth = 9;
    st2.wDay = 25;
    st2.wHour = 0;
    st2.wMinute = 0;
    st2.wSecond = 0; CTime Time1(st1);
    CTime Time2(st2);
    CTimeSpan ts = Time2 - Time1; //时间差
    int iTime = ts.GetDays(); //两个时间相隔的天数,小于1天值为0
            iTime = ts.GetTotalHours(); //两个时间点相隔的总小时数上面手动填入时间是为了方便观看,CTimeSpan下面的方法很多,可以自己查一下