C++ ctime strftime() Function
Example
Represent the current date in different ways:
time_t timestamp;
char output[50];
struct tm * datetime;
time(×tamp);
datetime = localtime(×tamp);
strftime(output, 50, "%B %e, %Y", datetime);
cout << output << "\n";
strftime(output, 50, "%I:%M:%S %p", datetime);
cout << output << "\n";
strftime(output, 50, "%D", datetime);
cout << output << "\n";
strftime(output, 50, "%c", datetime);
cout << output << "\n";
Try it Yourself »
Definition and Usage
The strftime()
function writes a C-style string representation of a date and time (provided by a tm
structure) into a char
array. A format parameter specifies how the date and time are represented.
Note: Use the gmtime()
or localtime()
function to get a tm
structure from a timestamp.
The format string
The format string is copied into the array with each of its format specifiers replaced by a generated value. The table below lists all of the format specifiers:
Format Specifier | Description | Example |
---|---|---|
%a |
Short representation of the weekday | Fri |
%A |
Full representation of the weekday | Friday |
%b |
Short representation of the month name | Dec |
%B |
Full representation of the month name | December |
%c |
Full date and time representation | Fri Dec 17 14:30:01 2023 |
%C |
Century (equivalent to taking the first two digits of a 4-digit year) | 20 |
%d |
Day of the month with leading zero | 09 |
%D |
Date representation equivalent to %m/%d/%y |
12/17/23 |
%e |
Day of the month with leading spaces | 9 |
%F |
Date representation equivalent to %Y-%m-%d |
2023-12-17 |
%g |
2-digit week-based year (week-based years start at the beginning of a week) | 23 |
%G |
4-digit week-based year (week-based years start at the beginning of a week) | 2023 |
%h |
Short representation of the month name (equivalent to %b ) |
Dec |
%H |
24-hour format of an hour | 14 |
%I |
12-hour format of an hour | 02 |
%j |
Day of the year (from 0 through 365) | 351 |
%m |
Numeric representation of a month | 351 |
%M |
Minutes within an hour | 30 |
%n |
A \n new line character |
|
%p |
AM or PM | PM |
%r |
Full 12-hour time format | 02:30:01 PM |
%R |
24-hour time format equivalent to %H:%M |
14:30 |
%S |
Seconds within a minute | 01 |
%t |
A \t tab character |
|
%T |
Full 24-hour time format equivalent to %H:%M:%S |
14:30:01 |
%u |
Numeric representation of a day of the week (from 1 to 7 starting with Monday) | 7 |
%U |
Week of the year starting at 0, with week 1 beginning on the first Sunday of the year | 51 |
%V |
Week of the year starting at 1, with week 1 beginning on the first Monday of the year and any day in January before the first Monay belonging to the previous year | 50 |
%w |
Numeric representation of a day of the week (from 0 to 6 starting with Sunday) | 0 |
%W |
Week of the year starting at 0, with week 1 beginning on the first Monday of the year | 50 |
%x |
Locale-based date representation | 12/17/23 |
%X |
Locale-based time representation | 14:30:01 |
%y |
2-digit year representation | 23 |
%Y |
4-digit year representation | 2023 |
%z |
Numeric timezone offset | +0000 |
%Z |
Timezone name | GMT |
%% |
A % character |
% |
Syntax
strftime(char * destination, size_t size, const char * format, const struct tm * datetime);
The size_t
data type represents a non-negative integer.
Parameter Values
Parameter | Description |
---|---|
destination | Required. A char array in which to write the formatted date. |
size | Required. Specifies the amount of space available in the char array to write in, measured in characters. |
format | Required. Specifies how the date should be formatted. |
datetime | Required. A tm structure containing information about the date and time to be represented. |
Technical Details
Returns: | A number indicating how many characters were written to array. A return value of 0 indicates that there is not enough available space to write in. |
---|