Ctime format usage

Source: Internet
Author: User
Tags month name


You can use the ctime class to conveniently obtain the current system time and convert it to various formats.

TheFormatArgument consists of one or more codes; as inPrintf, The formatting codes are preceded by a percent sign (%). Characters that do not begin%Are copied unchangedStrdest.TheLc_timeCategory of the current locale affects the output formattingStrftime. (For more information onLc_time, See setlocale.) The formatting codesStrftimeAre listed below:

%
Abbreviated weekday name
%
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 inPrintfFunction,#Flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.

Format code Meaning
% #,% #,% # B,% # B,% # P,% # X,% # Z,% # Z,% # % #Flag is ignored.
% # C Long Date and Time Representation, appropriate for current locale. For example: "Tuesday, March 14,199 5, 12:41:29 ".
% # X Long date representation, appropriate to current locale. For example: "Tuesday, March 14,199 5 ".
% # 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 % s \ n", tmpbuf); _ strdate (tmpbuf ); printf ("OS Date: \ 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 % s ", _ ctime64 (& ltime);/* display UTC. */GMT = _ gmtime64 (& ltime); printf ("Coordinated Universal Time: \ t % s", asctime (GMT )); /* convert to Time Structure and adjust for PM if necessary. */today = _ localtime64 (& L Time); 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 %. 8 S % s \ n ", asctime (today) + 11, ampm);/* print additional time informatio N. */_ ftime64 (& tstruct); printf ("plus milliseconds: \ t % u \ n", tstruct. millitm); printf ("zone difference in hours from UTC: \ t % u \ n", tstruct. timezone/60); printf ("Time Zone name: \ t % s \ n", _ tzname [0]); printf ("Daylight Savings: \ t % s \ n ", tstruct. dstflag? "Yes": "no");/* Make time for noon on Christmas, 1993. */If (_ mktime64 (& Xmas )! = (_ Time64_t)-1) printf ("Christmas \ 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: 49os 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 200212-hour time: 02:15:49 pmplus milliseconds: 455 zone difference in hours from UTC: 8 Time Zone name: Pacific Standard timedaylight savings: nochristmas sat Dec 25 12:00:00 1993

The following is a simple example:

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); // two tables are generally used in minutes.

M_edit1 = sztime;

Updatedata (false );

Settimer (1, 1000, null );

Msg1 = T. Format ("% d-% m-% Y"); // you can see the format function.

MessageBox (msg1 );

For the parameter meanings in format, see the preceding description.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.