std::strftime - cppreference.com

std::size_tstrftime(char*str,std::size_tcount,constchar*format,conststd::tm*tp);Converts the date and time information from a given calendar time tp to a null-terminated multibyte character string str according to

format string

format. Up to count bytes are written.

Parameters

str - pointer to the first element of the char array for output count - maximum number of bytes to write format - pointer to a null-terminated multibyte character string specifying the

format of conversion

tp - pointer to the object containing date and time information to be converted Format string

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:

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 Ywrites year as a decimal number, e.g. 2017 tm_yearEY
(C++11)writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale tm_yearywrites last 2 digits of year as a decimal number (range [00,99]) tm_yearOy
(C++11)writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale tm_yearEy
(C++11)writes year as offset from locale's alternative calendar period %EC (locale-dependent) tm_yearC
(C++11)writes first 2 digits of year as a decimal number (range [00,99]) tm_yearEC
(C++11)writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP tm_yearG
(C++11)writes ISO 8601 week-based year, i.e. the year that contains the specified week. In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4

Includes first Thursday of the year

tm_year, tm_wday, tm_ydayg
(C++11)writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]). In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4

Includes first Thursday of the year

tm_year, tm_wday, tm_ydayMonth bwrites abbreviated month name, e.g. Oct (locale dependent) tm_monh
(C++11)synonym of btm_monBwrites full month name, e.g. October (locale dependent) tm_monmwrites month as a decimal number (range [01,12]) tm_monOm
(C++11)writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale tm_monWeek Uwrites week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_ydayOU
(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_ydayWwrites week of the year as a decimal number (Monday is the first day of the week) (range [00,53]) tm_year, tm_wday, tm_ydayOW
(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_ydayV
(C++11)writes ISO 8601 week of the year (range [01,53]). In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4

Includes first Thursday of the year

tm_year, tm_wday, tm_ydayOV
(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_ydayDay of the year/month jwrites day of the year as a decimal number (range [001,366]) tm_ydaydwrites day of the month as a decimal number (range [01,31]) tm_mdayOd
(C++11)writes zero-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.

tm_mdaye
(C++11)writes day of the month as a decimal number (range [1,31]). Single digit is preceded by a space.

tm_mdayOe
(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.

tm_mdayDay of the week awrites abbreviated weekday name, e.g. Fri (locale dependent) tm_wdayAwrites full weekday name, e.g. Friday (locale dependent) tm_wdaywwrites weekday as a decimal number, where Sunday is 0 (range [0-6]) tm_wdayOw
(C++11)writes weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wdayu
(C++11)writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) tm_wdayOu
(C++11)writes weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale tm_wdayHour, minute, second Hwrites hour as a decimal number, 24 hour clock (range [00-23]) tm_hourOH
(C++11)writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale tm_hourIwrites hour as a decimal number, 12 hour clock (range [01,12]) tm_hourOI
(C++11)writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale tm_hourMwrites minute as a decimal number (range [00,59]) tm_minOM
(C++11)writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale tm_minSwrites second as a decimal number (range [00,60]) tm_secOS
(C++11)writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale tm_secOther cwrites 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 xwrites 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 Xwrites localized time representation, e.g. 18:40:20 or 6:40:20 PM (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_yearF
(C++11)equivalent to "%Y-%m-%d" (the ISO 8601 date format) tm_mon, tm_mday, tm_yearr
(C++11)writes localized 12-hour clock time (locale dependent) tm_hour, tm_min, tm_secR
(C++11)equivalent to "%H:%M"tm_hour, tm_minT
(C++11)equivalent to "%H:%M:%S" (the ISO 8601 time format) tm_hour, tm_min, tm_secpwrites localized a.m. or p.m. (locale dependent) tm_hourz
(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_isdstZwrites locale-dependent time zone name or abbreviation, or no characters if the time zone information is not available tm_isdstReturn value

The 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 indeterminate.

Example

Run this code

#include<ctime>#include<iostream>#include<iterator>#include<locale>voidutcExample(){// Example of the very popular RFC 3339 format UTC timestd::time_ttime=std::time({});chartimeString[std::size("yyyy-mm-ddThh:mm:ssZ")];std::strftime(std::data(timeString),std::size(timeString),"%FT%TZ",std::gmtime(&time));std::cout<<timeString<<'\n';}intmain(){std::time_tt=std::time(nullptr);charmbstr[100];if(std::strftime(mbstr,sizeof(mbstr),"%A %c",std::localtime(&t)))std::cout<<mbstr<<'\n';std::locale::global(std::locale("ja_JP.utf8"));if(std::strftime(mbstr,sizeof(mbstr),"%A %c",std::localtime(&t)))std::cout<<mbstr<<'\n';utcExample();}Possible output:

Tuesday Tue Sep 7 19:40:35 2021 火曜日 2021年09月07日 19時40分35秒 2021-09-07T19:40:35Z See also

asctime

(deprecated in C++26)

converts a

std::tm

object to a textual representation
(function)

[edit]

ctime

(deprecated in C++26)

converts a

std::time_t

object to a textual representation
(function)

[edit]

wcsftime

converts a

std::tm

object to custom wide string textual representation
(function)

[edit]

put_time

(C++11)

formats and outputs a date/time value according to the specified format
(function template)

[edit]

std::formatter<std::chrono::hh_mm_ss>

(C++20)

formatting support for hh_mm_ss
(class template specialization)

[edit]

C documentation

for strftime