`
huozheleisi
  • 浏览: 1236175 次
文章分类
社区版块
存档分类
最新评论

CTime类format的使用

 
阅读更多
使用CTime类可以很方便地取得当前系统时间并转换为各种格式

Theformatargument consists of one or more codes; as inprintf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with%are copied unchanged tostrDest.TheLC_TIMEcategory of the current locale affects the output formatting ofstrftime.(For more information onLC_TIME, seesetlocale.) The formatting codes forstrftimeare listed below:

%a
Abbreviated weekday name
%A
Full weekday name
%b
Abbreviated month name
%B
Full month name
%c
Date and time representation appropriate for locale
%d
Day of month as decimal number (01 – 31)
%H
Hour in 24-hour format (00 – 23)
%I
Hour in 12-hour format (01 – 12)
%j
Day of year as decimal number (001 – 366)
%m
Month as decimal number (01 – 12)
%M
Minute as decimal number (00 – 59)
%p
Current locale's A.M./P.M. indicator for 12-hour clock
%S
Second as decimal number (00 – 59)
%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w
Weekday as decimal number (0 – 6; Sunday is 0)
%W
Week of year as decimal number, with Monday as first day of week (00 – 53)
%x
Date representation for current locale
%X
Time representation for current locale
%y
Year without century, as decimal number (00 – 99)
%Y
Year with century, as decimal number
%z,%Z
Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%%
Percent sign

As in theprintffunction, the#flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.

Format code Meaning
%#a,%#A,%#b,%#B,%#p,%#X,%#z,%#Z,%#% #flag is ignored.
%#c Long date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29".
%#x Long date representation, appropriate to current locale. For example: "Tuesday, March 14, 1995".
%#d,%#H,%#I,%#j,%#m,%#M,%#S,%#U,%#w,%#W,%#y,%#Y Remove leading zeros (if any).

Requirements

Routine Required header Compatibility
strftime <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
wcsftime <time.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

Example

// crt_times.c
/* This program demonstrates these time and date functions:
 *      _time64         _ftime64        _ctime64     asctime
 *      _localtime64    _gmtime64       _mktime64    _tzset
 *      _strtime        _strdate        strftime
 *
 * Also the global variable:
 *      _tzname
 */

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

int main()
{
    char tmpbuf[128], ampm[] = "AM";
    __time64_t ltime;
    struct __timeb64 tstruct;
    struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

    /* Set time zone from TZ environment variable. If TZ is not set,
     * the operating system is queried to obtain the default value 
     * for the variable. 
     */
    _tzset();

    /* Display operating system-style date and time. */
    _strtime( tmpbuf );
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    _strdate( tmpbuf );
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

    /* Get UNIX-style time and display as number and string. */
    _time64( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    printf( "UNIX time and date:\t\t\t%s", _ctime64( &ltime ) );

    /* Display UTC. */
    gmt = _gmtime64( &ltime );
    printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

    /* Convert to time structure and adjust for PM if necessary. */
    today = _localtime64( &ltime );
    if( today->tm_hour >= 12 )
    {
   strcpy( ampm, "PM" );
   today->tm_hour -= 12;
    }
    if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
   today->tm_hour = 12;

    /* Note how pointer addition is used to skip the first 11 
     * characters and printf is used to trim off terminating 
     * characters.
     */
    printf( "12-hour time:\t\t\t\t%.8s %s\n",
       asctime( today ) + 11, ampm );

    /* Print additional time information. */
    _ftime64( &tstruct );
    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
    printf( "Zone difference in hours from UTC:\t%u\n", 
             tstruct.timezone/60 );
    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
    printf( "Daylight savings:\t\t\t%s\n", 
             tstruct.dstflag ? "YES" : "NO" );

    /* Make time for noon on Christmas, 1993. */
    if( _mktime64( &xmas ) != (__time64_t)-1 )
   printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

    /* Use time structure to build a customized time string. */
    today = _localtime64( &ltime );

    /* Use strftime to build a customized time string. */
    strftime( tmpbuf, 128,
         "Today is %A, day %d of %B in the year %Y.\n", today );
    printf( tmpbuf );
}

Sample Output

OS time:                                14:15:49
OS date:                                02/07/02
Time in seconds since UTC 1/1/70:       1013120149
UNIX time and date:                     Thu Feb 07 14:15:49 2002
Coordinated universal time:             Thu Feb 07 22:15:49 2002
12-hour time:                           02:15:49 PM
Plus milliseconds:                      455
Zone difference in hours from UTC:      8
Time zone name:                         Pacific Standard Time
Daylight savings:                       NO
Christmas                               Sat Dec 25 12:00:00 1993

简单点的如下:
CString msg1="aaaaaaaaaaa";

KillTimer(1);

CTime t = CTime::GetCurrentTime();
char szTime[8];
int nHour = t.GetHour();
int nMinute = t.GetMinute();
int nSecond = t.GetSecond();
wsprintf(szTime, "%02i:%02i:%02i", nHour, nMinute,nSecond);//分秒一般习惯用两位表
m_edit1=szTime;
UpdateData (FALSE);
SetTimer(1, 1000,NULL);
msg1=t.Format("%d-%m-%y"); //可以看到format的功能
MessageBox(msg1);

format中参数的含义见上面的说明

分享到:
评论

相关推荐

    CTime::Format参数使用说明

    十分详细的列举了CTime::Format的参数使用说明,比msdn详细点,希望对初学者有帮助

    VC++ 获取系统时间的方法汇总

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm.Format(现在时间是%Y年%m月%d日 %X); MessageBox(str,NULL,MB_OK); a,...

    VC 获取当前的日期、时间并格式化输出.rar

     m_time=CTime::GetCurrentTime();//获取当前时间日期  m_strDate=m_time.Format("%x");//格式化日期  m_strTime=m_time.Format("%X");//格式化时间  m_strDateTime=m_time.Format("%Y-%m-%d %H:%M:%S 第%W周...

    VC 获取并显示沪深股市实时交易行情.rar

    VC 6.0 获取并显示沪深股市实时交易行情,以Http Post方式请求URL,采集相关URL内容,... CString m_strTime = time.Format(" 刷新 %Y-%m-%d %H:%M:%S");  m_Text[31].SetWindowText(strArray.GetAt(31) m_strTime);

    Time基于MFC调用系统时间

    CTime time = CTime::GetCurrentTime(); //得到系统时间 m_strTime = time.Format("%Y-%m-%d %H:%M:%S"); SetTimer(1,1000,NULL); //设置1s的定时器 UpdateData(FALSE); GetDlgItem(IDC_EDIT_WINTIME)-&gt;...

    VC++常用函数

    省宽度,使用此值将为时间的显示预留空间)。注 2)在MainFrm.cpp中indicators声明处添加ID_INDICATOR_CLOCK,代 码如下:  这一步中ID_INDICATOR_CLOCK的插入位置将影响时间窗格在状态栏中 的显示位置。  3)安装...

    VC 计算两个时间点的时间间隔.rar

     m_strTime=m_time1.Format("%Y年%m月%d日 %H时%M分%S秒");  CTime m_time2(2010,1,1,0,0,0);//2010年元旦  CTimeSpan m_timespan=m_time2-m_time1;//获取时间间隔  m_Days=m_timespan.GetDays();//天数  m...

    Windows下目录监视程序代码

    CTime tt=CTime::GetCurrentTime(); CString strTT; strTT.Format("%d:%d:%d",tt.GetHour(),tt.GetMinute(),tt.GetSecond()); obj-&gt;m_list.InsertItem(0,obj-&gt;m_szi); obj-&gt;m_list....

    在Python中操作时间之strptime()方法的使用

    格式参数使用相同的指令使用strftime();它默认为“%a %b %d %H:%M:%S %Y”相匹配的ctime()所返回的格式。 如果字符串不能按格式进行解析,或者如果它具有解析后多余的数据,ValueError被挂起。 语法 以下是strptime...

    time-example.rar_Time_gmtime

    1.程序分析: ... printf(ctime(&lt)) /*english format output*/ printf(asctime(localtime(&lt))) /*tranfer to tm*/ printf(asctime(gmtime(&lt))) /*tranfer to Greenwich time*/ getch() }

    C语言基础

    【程序91】 题目:时间函数举例1 ... /*english format output*/ printf(asctime(localtime(&lt)));/*tranfer to tm*/ printf(asctime(gmtime(&lt))); /*tranfer to Greenwich time*/ getch(); }

    餐厅管理信息系统课程设计

    工程使用的是基于对话框的程序,每个对话框为一个类,父类为CDialog,实现的一些功能都封装在类的函数中,体现了面向对象编程语言C++的特性。 导入ADO接口:在工程的stdafx.h文件里直接引入符号#import引入ADO库文件...

    详解Python time库的使用

    &gt;&gt;&gt; time.ctime() 'Tue Oct 8 18:34:27 2019' &gt;&gt;&gt; time.gmtime() time.struct_time(tm_year=2019, tm_mon=10, tm_mday=8, tm_hour=10, tm_min=34, tm_sec=52, tm_wday=1, tm_yday=281, tm_isdst=0) 二、时间格式化 ...

    多线程入门实例VC++2005

    void ThreadFunc() { CTime time; CString strTime; m_bRun=TRUE;... strTime=time.Format("%H:%M:%S"); ::SetDlgItemText(AfxGetMainWnd()-&gt;m_hWnd,IDC_TIME,strTime); Sleep(1000); } }

    DT.zip_1970_between

    This software is a helper to learn format conversion between CTime formatted date-time and time_t long integer. time_t use number of seconds elapsed since midnight (00:00:00), January 1, 1970.

    C语言程序设计的几个详细的有用的实例

    /*english format output*/ printf(asctime(localtime(&lt;)));/*tranfer to tm*/ printf(asctime(gmtime(&lt;))); /*tranfer to Greenwich time*/ } 【程序92】 题目:时间函数举例2 1.程序分析: 2.程序源...

    ListView 应用示例

    //CTime; SYSTEMTIME sysTime; GetLocalTime(&sysTime); CString strTime; strTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime...

    (重要)AIX command 使用总结.txt

    F Format -&gt;以用户指定格式输出,Format参数为预定义或自定义设备对象类中的列名,如:name status等 H -&gt;显示列输出前面的头部分,即输出中包括列头部分 P -&gt;列出预定义设备对象类中设备的有关信息,即支持的设备,...

    c++播放器代码

    CTime c_time=CTime::GetCurrentTime(); CString nowtime=c_time.Format("%Y年%m月%d日 %H:%M:%S"); m_nowtime.SetWindowText(nowtime); this-&gt;Invalidate(TRUE); }break; case 2: { TCHAR buf[255]={'\0'};...

    Android实现倒计时的按钮的示例代码

    最近有人问我如何实现倒计时的按钮功能,例如发送验证码,我记得有个CountDownTimer,因为好久没用过了,自己就写了一个,代码如下 ... btn2.setText(String.format(%ds后重新发送验证码,millisUntilFinished/1000));

Global site tag (gtag.js) - Google Analytics