获取时间字符串

void SDlg::OnBnClickedBtnFull()
{
    this->ShowWindow(SW_MINIMIZE);
    Sleep(1000);

    int width = GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);    

      CString mSecond;
      //要包含头文件#include <sys/timeb.h>
      struct _timeb timebuffer;
      _ftime(&timebuffer);
      mSecond.Format("%d",timebuffer.millitm);

 
    char szFileName[512];
    sprintf(szFileName, "%s.%03d.png", CTime::GetCurrentTime().Format("%Y%m%d%H%M%S"),atoi(mSecond));
    int iRet = GetImg(0, 0, width, height, szFileName);
    if ( iRet == 0 )
        AfxMessageBox("ok");
    else
        AfxMessageBox("fails");

    this->ShowWindow(SW_SHOWNORMAL);
    ::SetWindowPos(m_hWnd, (HWND)-1, 0, 0, 0, 0, 3);
}

 

#include <time.h>
#include <sys/timeb.h>

struct
tm *ptm = NULL; time_t currtime; char s_currtime[9]; memset(s_currtime, 0x00, sizeof(s_currtime)); memset(&currtime, 0x00, sizeof(currtime)); time(&currtime); ptm = localtime(&currtime); sprintf(s_currtime, "%04d%02d%02d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday); #define LOG_DATE_SEPARATOR '-' memset(s_currtime, 0x00, sizeof(s_currtime)); //memset(&currtime, 0x00, sizeof(currtime)); //time(&currtime); //ptm = localtime(&currtime); ftime(&tb); ptm = localtime(&tb.time); sprintf(s_currtime, "%04d%c%02d%c%02d %02d%c%02d%c%02d.%03d", ptm->tm_year + 1900, LOG_DATE_SEPARATOR , ptm->tm_mon + 1, LOG_DATE_SEPARATOR , ptm->tm_mday, ptm->tm_hour, LOG_DATE_SEPARATOR , ptm->tm_min, LOG_DATE_SEPARATOR , ptm->tm_sec, tb.millitm);

 

#include <time.h>
#include <sys/timeb.h>

char* gettime()
{
    static char dt[50];

    struct _timeb tb;
    struct tm *ptm = NULL;
    time_t currtime;
    char s_currtime[9];
    
    memset(s_currtime, 0x00, sizeof(s_currtime));
    memset(&currtime, 0x00, sizeof(currtime));
    //time(&currtime);
    //ptm = localtime(&currtime);
    _ftime(&tb);
    ptm = localtime(&tb.time);

    sprintf(dt, "%04d%02d%02d-%02d:%02d:%02d.%03d", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
        ptm->tm_hour, ptm->tm_min, ptm->tm_sec, tb.millitm);

    return dt+8;
}

 

posted @ 2020-12-02 15:10  晨光静默  阅读(250)  评论(0编辑  收藏  举报