在VC中怎样获取当前时间,日期之类的?

解决方案 »

  1.   

    得到当前时间 日期
    CTime tTime = CTime::GetCurrentTime();
      

  2.   

    用SYSTEMTIME,可以精确到毫秒
    typedef struct _SYSTEMTIME {
        WORD wYear;
        WORD wMonth;
        WORD wDayOfWeek;
        WORD wDay;
        WORD wHour;
        WORD wMinute;
        WORD wSecond;
        WORD wMilliseconds;
    } SYSTEMTIME;GetSystemTime()函数用于取时间,SetSystemTime()用于设置系统时间,另外,你还可以用GetLocalTime()取得当地的当前时间,而不是UTC时间,当然,对应的有SetLocalTime函数;UTC时间和当地时间可以通过使用SetTimeZoneInformation函数设置当前时区参数来控制;用SystemTimeToFileTime函数可以将SYSTEMTIME格式转换为FILETIME格式;
      

  3.   

    总结楼上的:
    法一:    CTime time;
    time=CTime::GetCurrentTime();
    法二:GetSystemTime(&systime);
      

  4.   

    CTime time=CTime::GetCurrentTime();
    或者 
    CTime time=CTime::GetLocalTime();
      

  5.   

    CTime::GetCurrentTime
    Returns a CTime object that represents the current time.static CTime WINAPI GetCurrentTime( ) throw( );
    Example
    // Example for CTime::GetCurrentTime
    CTime t = CTime::GetCurrentTime();
      

  6.   

    CTime::GetDay
    Returns the day represent by the CTime object.int GetDay( ) const throw( );
    Return Value
    Returns the day of the month, based on local time, in the range 1 through 31.Res
    This function calls GetLocalTm, which uses an internal, statically allocated buffer. The data in this buffer is overwritten because of calls to other CTime member functions.Example
    // Example for CTime::GetDay, CTime::GetMonth, and CTime::GetYear
    CTime t( 1999, 3, 19, 22, 15, 0 ); // 10:15 PM March 19, 1999
    ATLASSERT( t.GetDay() == 19 );
    ATLASSERT( t.GetMonth() == 3 );
    ATLASSERT( t.GetYear() == 1999 );
      

  7.   

    谢谢,但是CTime类好像只能用来获得系统时间,我能不能将一个字符串的时间放在CTime对象里呢?因为CTime的成员函数都是Get开头,没有Set开头的。
      

  8.   

    你可以用COleDateTime,如下:
    COleVariant varTemp("2004-8-11");
    COleDateTime oleTime(varTemp);
    这样就可以将字符串“2004-8-11”最方便地转换为日期了。