我想获取当前时间,可以cstring str;str.format("%y%m%d,%h%M%s",...);这种格式显示出来

解决方案 »

  1.   

    SYSTEMTIME mytime;
    ::GetLocalTime(&mytime);
    year.Format(L"%04d-%02d-%02d",mytime.wYear,mytime.wMonth,mytime.wDay);
    day.Format(L"%02d:%02d:%02d",mytime.wHour,mytime.wMinute,mytime.wSecond);
      

  2.   

    COleDateTime t = COleDateTime::GetCurrentTime();
    CString str;
    str = t.Format("%Y%m%d,%H%M%S");
      

  3.   

    CTime t = CTime::GetCurrentTime(); 
    CString str = t.Format(_T("%Y-%m-%d,%H:%M:%S"));
    AfxMessageBox(str);
      

  4.   

    #ifndef _TM_DEFINED
    struct tm {
    int tm_sec; /* 秒 – 取值区间为[0,59] */
    int tm_min; /* 分 - 取值区间为[0,59] */
    int tm_hour; /* 时 - 取值区间为[0,23] */
    int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
    int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
    int tm_year; /* 年份,其值等于实际年份减去1900 */
    int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
    int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
    int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
    };
    #define _TM_DEFINED
    #endif
    /*
    gmtime()和localtime(),这两个函数的原型为:struct tm * gmtime(const time_t *timer);
    struct tm * localtime(const time_t * timer);其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,
    而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,
    那么我用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:
    */
    #include "time.h"
    #include "stdio.h"
    int main(void)
    {
    struct tm *local;
    time_t t;
    t=time(NUL);
    local=localtime(&t);
    printf("Local hour is: %d\n",local->tm_hour);
    local=gmtime(&t);
    printf("UTC hour is: %d\n",local->tm_hour);
    return 0;
    }
    /*
    运行结果是:Local hour is: 15
    UTC hour is: 7
    */
      

  5.   

    CTime m_time;
    m_time=CTime::GetCurrentTime();
    CString strTime=m_time.Format("%Y-%m-%d %H:%M:%S");//格式化日期时间