![]() |
![]() |
![]() |
C++ Reference: strftime()
strftime()
Declaration
size_t strftime(char* cpOutput,
size_t qMaxSize,
const char* kcpFormat,
const tm* kqpTimePtr);
Description
This function takes in a time specification through the pointer "kqpTimePtr" and a format request through the argument "kcpFormat" and outputs a text time description in the char array buffer that is passed in as "cpOutput." The argument "qMaxSize" is the maximum number of characters used in the description and the value returned is the actual length of the text description outputted in "cpOutput."
Format Table
| Token | Substitution | Range | Example |
| %a | Weekday (Abbreviated Name) | Sun, Mon, Tue, Wed, Thu, Fri, Sat | Mon |
| %A | Weekday (Full Name) | Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday | Sunday |
| %b | Month (Abbreviated Name) | Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec | Jan |
| %B | Month (Full Name) | January, February, March, April, May, June, July, August, September, October, November, December | February |
| %c | Date/Time Representation (Locale) | Any Date and Time | 10/10/18 08:19:38 |
| %d | Day of Month | [01, 31] | 03 |
| %H | Hour in 24 hour format | [00, 23] | 14 |
| %I | Hour in 12 hour format | [00, 11] | 09 |
| %j | Day of the year | [001, 366] | 034 |
| %m | Month (as number) | [01, 12] | 04 |
| %M | Minute | [00, 59] | 00 |
| %p | Ante Meridiem or Post Meridiem | AM, PM | AM |
| %S | Second | [00, 59] | 45 |
| %U | Week (as number) Sunday Start | [00, 53] | 07 |
| %w | Weekday (as number, 0 = Sunday) | [0, 6] | 5 |
| %W | Week (as number) Monday Start | [00, 53] | 43 |
| %x | Local Date Representation | Any Date | 10/10/18 |
| %X | Local Time Representation | Any Time | 08:19:38 |
| %y | Year Modulo 100 | [00, 99] | 24 |
| %Y | Year | Any Year | 2008 |
| %z | Time Zone (Locale) | Any Time Zone | Central Standard Time |
| %Z | Time Zone (Locale) | Any Time Zone | Central Standard Time |
| %% | Percent Sign | | |
| %#c | Long Date/Time Representation (Locale) | Any Date and Time | Saturday, October 18, 2008 08:32:11 |
| %#x | Long Date Representation (Locale) | Any Date | Saturday, October 18, 2008 |
| %#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y | Removes Leading Zeros | | |
| %#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% | Ignored | | |
Example
#include <iostream>
#include <ctime>
int main() {
using namespace std;
time_t qTime;
tm* qpTimeSpec;
// Get the number of seconds since midnight Jan. 1, 1970
time(&qTime);
// Convert the seconds to a tm struct variable
qpTimeSpec = gmtime(&qTime);
char caTime[100];
size_t qActualLength = strftime(caTime, 100, "%I:%M %p", qpTimeSpec);
cout << "Written length = " << qActualLength << endl;
cout << "Time = " << caTime << endl;
return 0;
}
Output:
![]() |
![]() |
![]() |
| Home | | | Reference | | | Play Games! | | | Blog | | | Forum | | | Site Map | | | Contact Us |





