【MFC】获取系统当前时间

来源:互联网 发布:手机管理网络的软件 编辑:程序博客网 时间:2024/06/05 02:51
void CExampleDlg::OnGettime(){CTime t = CTime::GetCurrentTime();int nYear = t.GetYear();int nMonth = t.GetMonth();int nDay = t.GetDay();int nHour = t.GetHour();int  nMinute = t.GetMinute();int nSecond = t.GetSecond();CString str;str.Format(_T("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d"),nYear,nMonth,nDay,nHour,nMinute,nSecond);AfxMessageBox(str);}

void CExampleDlg::OnTime2(){time_t tt = time(NULL);tm* pTime = localtime(&tt);int nYear = pTime->tm_year+1900;int nMonth = pTime->tm_mon+1;int nDay = pTime->tm_mday;int nHour = pTime->tm_hour;int nMinute = pTime->tm_min;int nSecond = pTime->tm_sec;CString str;str.Format(_T("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d"),nYear,nMonth,nDay,nHour,nMinute,nSecond);AfxMessageBox(str);}


tm结构体解释

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代表一月) - <span style="color:#cc0000;">取值区间为[0,11]</span> */   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()为负。*/    long int tm_gmtoff;  /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/     const char *tm_zone;     /*当前时区的名字(与环境变量TZ有关)*/    };   


原创粉丝点击