| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 |     <h1 id="firstHeading" class="firstHeading">strftime</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><time.h></code> </th> <th> </th> <th> </th> </tr>   <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">size_t strftime( char*          str, size_t count,
                 const char*          format, const struct tm*          tp );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">size_t strftime( char* restrict str, size_t count,
                 const char* restrict format, const struct tm* restrict tp );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>   </table> <p>Converts the date and time information from a given calendar time <code>tp</code> to a null-terminated multibyte character string <code>str</code> according to <a href="#Format_string">format string</a> <code>format</code>. Up to <code>count</code> bytes are written.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the first element of the char array for output </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of bytes to write </td>
</tr> <tr class="t-par"> <td> format </td> <td> - </td> <td> pointer to a null-terminated multibyte character string specifying the <a href="#Format_string">format of conversion</a> </td>
</tr> <tr class="t-par"> <td> tp </td> <td> - </td> <td> pointer to a <code><span class="kw1">struct</span> <a href="http://en.cppreference.com/w/c/chrono/tm"><span class="kw522">tm</span></a></code> object specifying the time to format </td>
</tr>
</table> <h3 id="Format_string"> Format string</h3> <p>The format string consists of zero or more conversion specifiers and ordinary characters (except <code>%</code>). All ordinary characters, including the terminating null character, are copied to the output string without modification. Each conversion specification begins with <code>%</code> character, optionally followed by <code>E</code> or <code>O</code> modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The following format specifiers are available:</p>
<table class="wikitable"> <tr> <th> Conversion<br> specifier </th> <th> Explanation </th> <th> Used fields </th>
</tr> <tr> <th> <code>%</code> </th> <td> writes literal <code>%</code>. The full conversion specification must be <code>%%</code>. </td> <td> </td>
</tr> <tr> <th> <code>n</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes newline character </td> <td> </td>
</tr> <tr> <th> <code>t</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes horizontal tab character </td> <td> </td>
</tr> <tr> <th colspan="3"> Year </th>
</tr> <tr> <th> <code>Y</code> </th> <td> writes <b>year</b> as a decimal number, e.g. 2017 </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>EY</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>year</b> in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>y</code> </th> <td> writes last 2 digits of <b>year</b> as a decimal number (range <code>[00,99]</code>) </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>Oy</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes last 2 digits of <b>year</b> using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>Ey</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>year</b> as offset from locale's alternative calendar period <code>%EC</code> (locale-dependent) </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>C</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes first 2 digits of <b>year</b> as a decimal number (range <code>[00,99]</code>) </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>EC</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes name of the <b>base year (period)</b> in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP </td> <td> <code>tm_year</code> </td>
</tr> <tr> <th> <code>G</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>ISO 8601 week-based year</b>, i.e. the year that contains the specified week. <p>In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:</p>
<ul>
<li> Includes January 4 </li>
<li> Includes first Thursday of the year </li>
</ul> </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>g</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes last 2 digits of <b>ISO 8601 week-based year</b>, i.e. the year that contains the specified week (range <code>[00,99]</code>). <p>In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:</p>
<ul>
<li> Includes January 4 </li>
<li> Includes first Thursday of the year </li>
</ul> </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th colspan="3"> Month </th>
</tr> <tr> <th> <code>b</code> </th> <td> writes <b>abbreviated month</b> name, e.g. <code>Oct</code> (locale dependent) </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>Ob</code><br><span class="t-mark-rev t-since-c23">(C23)</span> </th> <td> writes <b>abbreviated month</b> name in the locale's alternative representation </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>h</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> synonym of <code>b</code> </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>B</code> </th> <td> writes <b>full month</b> name, e.g. <code>October</code> (locale dependent) </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>OB</code><br><span class="t-mark-rev t-since-c23">(C23)</span> </th> <td> writes appropriate <b>full month</b> name in the locale's alternative representation </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>m</code> </th> <td> writes <b>month</b> as a decimal number (range <code>[01,12]</code>) </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th> <code>Om</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>month</b> using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale </td> <td> <code>tm_mon</code> </td>
</tr> <tr> <th colspan="3"> Week </th>
</tr> <tr> <th> <code>U</code> </th> <td> writes <b>week of the year</b> as a decimal number (Sunday is the first day of the week) (range <code>[00,53]</code>) </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>OU</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>week of the year</b>, as by <code>%U</code>, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>W</code> </th> <td> writes <b>week of the year</b> as a decimal number (Monday is the first day of the week) (range <code>[00,53]</code>) </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>OW</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>week of the year</b>, as by <code>%W</code>, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>V</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>ISO 8601 week of the year</b> (range <code>[01,53]</code>). <p>In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:</p>
<ul>
<li> Includes January 4 </li>
<li> Includes first Thursday of the year </li>
</ul> </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th> <code>OV</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>week of the year</b>, as by <code>%V</code>, using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale </td> <td> <code>tm_year</code>, <code>tm_wday</code>, <code>tm_yday</code> </td>
</tr> <tr> <th colspan="3"> Day of the year/month </th>
</tr> <tr> <th> <code>j</code> </th> <td> writes <b>day of the year</b> as a decimal number (range <code>[001,366]</code>) </td> <td> <code>tm_yday</code> </td>
</tr> <tr> <th> <code>d</code> </th> <td> writes <b>day of the month</b> as a decimal number (range <code>[01,31]</code>) </td> <td> <code>tm_mday</code> </td>
</tr> <tr> <th> <code>Od</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes zero-based <b>day of the month</b> using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale <p>Single character is preceded by a space.</p>
</td> <td> <code>tm_mday</code> </td>
</tr> <tr> <th> <code>e</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>day of the month</b> as a decimal number (range <code>[1,31]</code>). <p>Single digit is preceded by a space.</p>
</td> <td> <code>tm_mday</code> </td>
</tr> <tr> <th> <code>Oe</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes one-based <b>day of the month</b> using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale <p>Single character is preceded by a space.</p>
</td> <td> <code>tm_mday</code> </td>
</tr> <tr> <th colspan="3"> Day of the week </th>
</tr> <tr> <th> <code>a</code> </th> <td> writes <b>abbreviated weekday</b> name, e.g. <code>Fri</code> (locale dependent) </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th> <code>A</code> </th> <td> writes <b>full weekday</b> name, e.g. <code>Friday</code> (locale dependent) </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th> <code>w</code> </th> <td> writes <b>weekday</b> as a decimal number, where Sunday is <code>0</code> (range <code>[0-6]</code>) </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th> <code>Ow</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>weekday</b>, where Sunday is <code>0</code>, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th> <code>u</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>weekday</b> as a decimal number, where Monday is <code>1</code> (ISO 8601 format) (range <code>[1-7]</code>) </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th> <code>Ou</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>weekday</b>, where Monday is <code>1</code>, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale </td> <td> <code>tm_wday</code> </td>
</tr> <tr> <th colspan="3"> Hour, minute, second </th>
</tr> <tr> <th> <code>H</code> </th> <td> writes <b>hour</b> as a decimal number, 24 hour clock (range <code>[00-23]</code>) </td> <td> <code>tm_hour</code> </td>
</tr> <tr> <th> <code>OH</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>hour</b> from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale </td> <td> <code>tm_hour</code> </td>
</tr> <tr> <th> <code>I</code> </th> <td> writes <b>hour</b> as a decimal number, 12 hour clock (range <code>[01,12]</code>) </td> <td> <code>tm_hour</code> </td>
</tr> <tr> <th> <code>OI</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>hour</b> from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale </td> <td> <code>tm_hour</code> </td>
</tr> <tr> <th> <code>M</code> </th> <td> writes <b>minute</b> as a decimal number (range <code>[00,59]</code>) </td> <td> <code>tm_min</code> </td>
</tr> <tr> <th> <code>OM</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>minute</b> using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale </td> <td> <code>tm_min</code> </td>
</tr> <tr> <th> <code>S</code> </th> <td> writes <b>second</b> as a decimal number (range <code>[00,60]</code>) </td> <td> <code>tm_sec</code> </td>
</tr> <tr> <th> <code>OS</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>second</b> using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale </td> <td> <code>tm_sec</code> </td>
</tr> <tr> <th colspan="3"> Other </th>
</tr> <tr> <th> <code>c</code> </th> <td> writes <b>standard date and time string</b>, e.g. <code>Sun Oct 17 04:41:13 2010</code> (locale dependent) </td> <td> all </td>
</tr> <tr> <th> <code>Ec</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>alternative date and time string</b>, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale </td> <td> all </td>
</tr> <tr> <th> <code>x</code> </th> <td> writes localized <b>date representation</b> (locale dependent) </td> <td> all </td>
</tr> <tr> <th> <code>Ex</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>alternative date representation</b>, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale </td> <td> all </td>
</tr> <tr> <th> <code>X</code> </th> <td> writes localized <b>time representation</b>, e.g. 18:40:20 or 6:40:20 PM (locale dependent) </td> <td> all </td>
</tr> <tr> <th> <code>EX</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>alternative time representation</b> (locale dependent) </td> <td> all </td>
</tr> <tr> <th> <code>D</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> equivalent to <b>"%m/%d/%y"</b> </td> <td> <code>tm_mon</code>, <code>tm_mday</code>, <code>tm_year</code> </td>
</tr> <tr> <th> <code>F</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> equivalent to <b>"%Y-%m-%d"</b> (the ISO 8601 date format) </td> <td> <code>tm_mon</code>, <code>tm_mday</code>, <code>tm_year</code> </td>
</tr> <tr> <th> <code>r</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes localized <b>12-hour clock</b> time (locale dependent) </td> <td> <code>tm_hour</code>, <code>tm_min</code>, <code>tm_sec</code> </td>
</tr> <tr> <th> <code>R</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> equivalent to <b>"%H:%M"</b> </td> <td> <code>tm_hour</code>, <code>tm_min</code> </td>
</tr> <tr> <th> <code>T</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> equivalent to <b>"%H:%M:%S"</b> (the ISO 8601 time format) </td> <td> <code>tm_hour</code>, <code>tm_min</code>, <code>tm_sec</code> </td>
</tr> <tr> <th> <code>p</code> </th> <td> writes localized <b>a.m. or p.m.</b> (locale dependent) </td> <td> <code>tm_hour</code> </td>
</tr> <tr> <th> <code>z</code><br><span class="t-mark-rev t-since-c99">(C99)</span> </th> <td> writes <b>offset from UTC</b> in the ISO 8601 format (e.g. <code>-0430</code>), or no characters if the time zone information is not available </td> <td> <code>tm_isdst</code> </td>
</tr> <tr> <th> <code>Z</code> </th> <td> writes locale-dependent <b>time zone name or abbreviation</b>, or no characters if the time zone information is not available </td> <td> <code>tm_isdst</code> </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>The number of bytes written into the character array pointed to by <code>str</code> not including the terminating <code>'\0'</code> on success. If <code>count</code> was reached before the entire string could be stored, <code>0</code> is returned and the contents are indeterminate.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <locale.h>
#include <stdio.h>
#include <time.h>
 
int main(void)
{
    char buff[70];
    struct tm my_time = { .tm_year=112, // = year 2012
                          .tm_mon=9,    // = 10th month
                          .tm_mday=9,   // = 9th day
                          .tm_hour=8,   // = 8 hours
                          .tm_min=10,   // = 10 minutes
                          .tm_sec=20    // = 20 secs
    };
 
    if (strftime(buff, sizeof buff, "%A %c", &my_time))
        puts(buff);
    else
        puts("strftime failed");
 
    setlocale(LC_TIME, "el_GR.utf8");
 
    if (strftime(buff, sizeof buff, "%A %c", &my_time))
        puts(buff);
    else
        puts("strftime failed");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Sunday Sun Oct  9 08:10:20 2012
Κυριακή Κυρ 09 Οκτ 2012 08:10:20 πμ EST</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.27.3.5 The strftime function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.27.3.5 The strftime function (p: 288-291) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.27.3.5 The strftime function (p: 394-397) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.23.3.5 The strftime function (p: 343-347) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.12.3.5 The strftime function </li></ul>
</ul>                   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="asctime" title="c/chrono/asctime"> <span class="t-lines"><span>asctime</span><span>asctime_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a <code><a href="tm" title="c/chrono/tm">tm</a></code> object to a textual representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="ctime" title="c/chrono/ctime"> <span class="t-lines"><span>ctime</span><span>ctime_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a <code><a href="time_t" title="c/chrono/time t">time_t</a></code> object to a textual representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="wcsftime" title="c/chrono/wcsftime"> <span class="t-lines"><span>wcsftime</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> converts a <code><a href="tm" title="c/chrono/tm">tm</a></code> object to custom wide string textual representation <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/chrono/c/strftime" title="cpp/chrono/c/strftime">C++ documentation</a></span> for <code>strftime</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/chrono/strftime" class="_attribution-link">https://en.cppreference.com/w/c/chrono/strftime</a>
  </p>
</div>
 |