strftime

来自cppreference.com
< c‎ | chrono

 
 
日期和时间工具
功能
原文:
Functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
时间操作
原文:
Time manipulation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
difftime
time
clock
格式转换
原文:
Format conversions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
asctime
ctime
strftime
wcsftime
gmtime
localtime
mktime
常量
原文:
Constants
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
CLOCKS_PER_SEC
类型
原文:
Types
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm
time_t
clock_t
 
在头文件 <time.h> 中定义
size_t strftime( char* str, size_t count, const char* format, tm* time );
转换的日期和时间信息从一个给定的日历时间timenull结尾的多字节字符串str根据格式字符串format的。 count字节写的
原文:
Converts the date and time information from a given calendar time time to a null-terminated multibyte character string str according to format string format. Up to count bytes are written.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 参数

str -
指针,用于输出char数组的第一个元素
原文:
pointer to the first element of the char array for output
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
count -
要写入的字节的最大数量
原文:
maximum number of bytes to write
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
format -
指针null结尾的多字节字符的字符串,该字符串指定的格式转换。
格式字符串由零个或多个转换符和普通字符(除%)。所有普通字符,包括终止空字符,不加修改地复制到输出字符串。每个转换规格%字符开始,后面可以跟EO的修饰符(被忽略,如果不支持的语言环境),其次的性格,决定了行为的说明。下面的格式说明符
原文:
The format string consists of zero or more conversion specifiers and ordinary characters (except %). All ordinary characters, including the terminating null character, are copied to the output string without modification. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The following format specifiers are available:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
Conversion
specifier
Explanation Used fields
% writes literal %. The full conversion specification must be %%.
n(C++11) writes newline character
t(C++11) writes horizontal tab character
Year
Y writes year as a 4 digit decimal number tm_year
EY(C++11) writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale tm_year
y writes last 2 digits of year as a decimal number (range [00,99]) tm_year
Oy(C++11) writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale tm_year
Ey(C++11) writes year as offset from locale's alternative calendar period %EC (locale-dependent) tm_year
C(C++11) writes first 2 digits of year as a decimal number (range [00,99]) tm_year
EC(C++11) writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP tm_year
G(C++11) writes ISO 8601 week-based year, i.e. the year that contains the specified week.
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
g(C++11) writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]).
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
Month
b writes abbreviated month name, e.g. Oct (locale dependent) tm_mon
h(C++11) synonym of b tm_mon
B writes full month name, e.g. October (locale dependent) tm_mon
m writes month as a decimal number (range [01,12]) tm_mon
Om(C++11) writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale tm_mon
Week
U writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_yday
OU(C++11) writes week of the year, as by %U, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
W writes week of the year as a decimal number (Monday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_yday
OW(C++11) writes week of the year, as by %W, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
V(C++11) writes ISO 8601 week of the year (range [01,53]).
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
OV(C++11) writes week of the year, as by %V, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
Day of the year/month
j writes day of the year as a decimal number (range [001,366]) tm_yday
d writes day of the month as a decimal number (range [01,31]) tm_mday
Od(C++11) writes zero-based day of the month using the alternative numeric system, e.g 二十七 instead of 23 in ja_JP locale
单字符前面的空间.
原文:
Single character is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
e(C++11) writes day of the month as a decimal number (range [1,31]).
单数字前会加上一个空格.
原文:
Single digit is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
Oe(C++11) writes one-based day of the month using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale
单字符前面的空间.
原文:
Single character is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
Day of the week
a writes abbreviated weekday name, e.g. Fri (locale dependent) tm_wday
A writes full weekday name, e.g. Friday (locale dependent) tm_wday
w writes weekday as a decimal number, where Sunday is 0 (range [0-6]) tm_wday
Ow(C++11) writes weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wday
u(C++11) writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) tm_wday
Ou(C++11) writes weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wday
Hour, minute, second
H writes hour as a decimal number, 24 hour clock (range [00-23]) tm_hour
OH(C++11) writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale tm_hour
I writes hour as a decimal number, 12 hour clock (range [01,12]) tm_hour
OI(C++11) writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale tm_hour
M writes minute as a decimal number (range [00,59]) tm_min
OM(C++11) writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale tm_min
S writes second as a decimal number (range [00,60]) tm_sec
OS(C++11) writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale tm_sec
Other
c writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) all
Ec(C++11) writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all
x writes localized date representation (locale dependent) all
Ex(C++11) writes alternative date representation, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all
X writes localized time representation (locale dependent) all
EX(C++11) writes alternative time representation (locale dependent) all
D(C++11) equivalent to "%m/%d/%y" tm_mon, tm_mday, tm_year
F(C++11) equivalent to "%Y-%m-%d" (the ISO 8601 date format) tm_mon, tm_mday, tm_year
r(C++11) writes localized 12-hour clock time (locale dependent) tm_hour, tm_min, tm_sec
R(C++11) equivalent to "%H:%M" tm_hour, tm_min
T(C++11) equivalent to "%H:%M:%S" (the ISO 8601 time format) tm_hour, tm_min, tm_sec
p writes localized a.m. or p.m. (locale dependent) tm_hour
z(C++11) writes offset from UTC in the ISO 8601 format (e.g. -0430), or no characters if the time zone information is not available tm_isdst
Z writes time zone name or abbreviation, or no characters if the time zone information is not available (locale dependent) tm_isdst
原文:
pointer to a null-terminated multibyte character string specifying the format of conversion.
格式字符串由零个或多个转换符和普通字符(除%)。所有普通字符,包括终止空字符,不加修改地复制到输出字符串。每个转换规格%字符开始,后面可以跟EO的修饰符(被忽略,如果不支持的语言环境),其次的性格,决定了行为的说明。下面的格式说明符
原文:
The format string consists of zero or more conversion specifiers and ordinary characters (except %). All ordinary characters, including the terminating null character, are copied to the output string without modification. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The following format specifiers are available:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
Conversion
specifier
Explanation Used fields
% writes literal %. The full conversion specification must be %%.
n(C++11) writes newline character
t(C++11) writes horizontal tab character
Year
Y writes year as a 4 digit decimal number tm_year
EY(C++11) writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale tm_year
y writes last 2 digits of year as a decimal number (range [00,99]) tm_year
Oy(C++11) writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale tm_year
Ey(C++11) writes year as offset from locale's alternative calendar period %EC (locale-dependent) tm_year
C(C++11) writes first 2 digits of year as a decimal number (range [00,99]) tm_year
EC(C++11) writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP tm_year
G(C++11) writes ISO 8601 week-based year, i.e. the year that contains the specified week.
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
g(C++11) writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]).
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
Month
b writes abbreviated month name, e.g. Oct (locale dependent) tm_mon
h(C++11) synonym of b tm_mon
B writes full month name, e.g. October (locale dependent) tm_mon
m writes month as a decimal number (range [01,12]) tm_mon
Om(C++11) writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale tm_mon
Week
U writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_yday
OU(C++11) writes week of the year, as by %U, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
W writes week of the year as a decimal number (Monday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_yday
OW(C++11) writes week of the year, as by %W, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
V(C++11) writes ISO 8601 week of the year (range [01,53]).
在IS0 8601周(星期一)开始,并在今年的第一周必须满足下列要求:
原文:
In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括1月4日
    原文:
    Includes January 4
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 包括在今年的第一个星期四
    原文:
    Includes first Thursday of the year
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_year, tm_wday, tm_yday
OV(C++11) writes week of the year, as by %V, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale tm_year, tm_wday, tm_yday
Day of the year/month
j writes day of the year as a decimal number (range [001,366]) tm_yday
d writes day of the month as a decimal number (range [01,31]) tm_mday
Od(C++11) writes zero-based day of the month using the alternative numeric system, e.g 二十七 instead of 23 in ja_JP locale
单字符前面的空间.
原文:
Single character is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
e(C++11) writes day of the month as a decimal number (range [1,31]).
单数字前会加上一个空格.
原文:
Single digit is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
Oe(C++11) writes one-based day of the month using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale
单字符前面的空间.
原文:
Single character is preceded by a space.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
tm_mday
Day of the week
a writes abbreviated weekday name, e.g. Fri (locale dependent) tm_wday
A writes full weekday name, e.g. Friday (locale dependent) tm_wday
w writes weekday as a decimal number, where Sunday is 0 (range [0-6]) tm_wday
Ow(C++11) writes weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wday
u(C++11) writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) tm_wday
Ou(C++11) writes weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wday
Hour, minute, second
H writes hour as a decimal number, 24 hour clock (range [00-23]) tm_hour
OH(C++11) writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale tm_hour
I writes hour as a decimal number, 12 hour clock (range [01,12]) tm_hour
OI(C++11) writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale tm_hour
M writes minute as a decimal number (range [00,59]) tm_min
OM(C++11) writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale tm_min
S writes second as a decimal number (range [00,60]) tm_sec
OS(C++11) writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale tm_sec
Other
c writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) all
Ec(C++11) writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all
x writes localized date representation (locale dependent) all
Ex(C++11) writes alternative date representation, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale all
X writes localized time representation (locale dependent) all
EX(C++11) writes alternative time representation (locale dependent) all
D(C++11) equivalent to "%m/%d/%y" tm_mon, tm_mday, tm_year
F(C++11) equivalent to "%Y-%m-%d" (the ISO 8601 date format) tm_mon, tm_mday, tm_year
r(C++11) writes localized 12-hour clock time (locale dependent) tm_hour, tm_min, tm_sec
R(C++11) equivalent to "%H:%M" tm_hour, tm_min
T(C++11) equivalent to "%H:%M:%S" (the ISO 8601 time format) tm_hour, tm_min, tm_sec
p writes localized a.m. or p.m. (locale dependent) tm_hour
z(C++11) writes offset from UTC in the ISO 8601 format (e.g. -0430), or no characters if the time zone information is not available tm_isdst
Z writes time zone name or abbreviation, or no characters if the time zone information is not available (locale dependent) tm_isdst
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 返回值

所指向的字符数组写入的字节数str不包括终止'\0'成功。 count之前达成的整个字符串可以存储,0返回的内容是不确定的.....
原文:
number of bytes written into the character array pointed to by str not including the terminating '\0' on success. If count was reached before the entire string could be stored, 0 is returned and the contents are undefined.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 示例

#include <stdio.h>
#include <time.h>
 
int main()
{
    char buff[50] = {0};
    struct tm _tm = {0};
    _tm.tm_year=112; // = year 2012
    _tm.tm_mon=9;    // = 10th month
    _tm.tm_mday=9;   // = 9th day
    _tm.tm_hour=8;   // = 8 hours
    _tm.tm_min=10;   // = 10 minutes
    _tm.tm_sec=20;   // = 20 secs
    if(strftime(buff,50,"%Y-%m-%d %H:%M:%S",&_tm) != 19)
        puts("Error!");
    else
        puts(buff);
 
    return 0;
}

输出:

2012-10-09 08:10:20

[编辑] 另请参阅

一个time_t对象转换为文本表示
原文:
converts a time_t object to a textual representation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

(函数) [edit]
一个tm对象转换为文本表示
原文:
converts a tm object to a textual representation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

(函数) [edit]
自定义的宽字符串的文字表述转换为tm对象
原文:
converts a tm object to custom wide string textual representation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

(函数) [edit]
C++ documentation for strftime