Strftime () function usage

Source: Internet
Author: User
Tags month name ranges

I made a DLL and want to call the ctime class. The ctime class cannot be used in the MFC dynamic link library because it uses the strftime function not supported by the Windows dynamic link library. You have to solve the problem by using the method in <time. h>.

Time_t t = time (0 );
Char TMP [64];
Strftime (TMP, sizeof (TMP), "% Y % m % d % H % m % s", localtime (& T); // format the output.

Output result: 20080912132328

 

We can use the strftime () function to format the time as we want. Its prototype is as follows:

Size_t strftime (
Char * strdest,
Size_t maxsize,
Const char * format,
Const struct TM * timeptr
);

You can place the time information stored in timeptr in the strdest string based on the format command in the string to which the strdest points. A maximum of maxsize characters can be stored in strdest. This function returns the number of characters placed in the string pointing to strdest.

The strftime () function is similar to sprintf (): recognizes the command set starting with the percent sign (%), and formats the output result in a string. The formatting command specifies the exact representation of various date and time information in strdest. Other characters in the format string are put into the string as they are. Format Commands are listed below, which are case sensitive.

% A abbreviation of the day of the week
% A full name of the day of the week
Abbreviation of % B month
% B full name
% C standard date time string
The last two digits of the Year % C
% D indicates the day of the month in decimal format.
% D month/day/year
% E indicates the day of the month in decimal format in the two-character field
% F-month-day
The last two digits of the Year % G. The year is based on the week.
% G year, based on the week Year
% H abbreviated month name
% H in 24-hour format
% I 12-hour
% J indicates the day of the year in decimal format.
Month in % m decimal format
% M decimal number of minutes
% N new line operator
% P equivalent display of local am or PM
% R 12 hours
% R display hour and minute: hh: mm
% S decimal seconds
% T horizontal Tab
% T display time in seconds: hh: mm: SS
% U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0)
% U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53)
% V indicates the week of the year, which is based on the year of the week.
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0)
% W indicates the week of the year. Monday is the first day (from 0 to 53)
% X standard date string
% X standard time string
% Y does not contain the century's decimal year (the value ranges from 0 to 99)
% Y indicates the tenth year of the century.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned.
% Percent sign

If you want to display the current time in 12-hour format, it is like the following program:

# I nclude "time. h"
# I nclude "stdio. h"
Int main (void)
{
Struct TM * PTR;
Time_t lt;
Char STR [80];
Lt = Time (null );
PTR = localtime (<);
Strftime (STR, 100, "it is now % I % P", PTR );
Printf (STR );
Return 0;
}

The running result is:
It is now 4

The following program shows the current complete date:

# I nclude
# I nclude

Void main (void)
{
Struct TM * newtime;
Char tmpbuf [128];
Time_t lt1;
Time (<1 );
Newtime = localtime (<1 );
Strftime (tmpbuf, 128, "Today is % A, day % d of % B in the year % Y./N", newtime );
Printf (tmpbuf );
}

Running result:

Today is Saturday, day 30 of July in the year 2005.

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.