C Standard Libraries C++

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


TokenSubstitutionRangeExample

%aWeekday (Abbreviated Name)Sun, Mon, Tue, Wed, Thu, Fri, SatMon
%AWeekday (Full Name)Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, SaturdaySunday
%bMonth (Abbreviated Name)Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, DecJan
%BMonth (Full Name)January, February, March, April, May, June, July, August, September, October, November, DecemberFebruary
%cDate/Time Representation (Locale)Any Date and Time10/10/18 08:19:38
%dDay of Month[01, 31]03
%HHour in 24 hour format[00, 23]14
%IHour in 12 hour format[00, 11]09
%jDay of the year[001, 366]034
%mMonth (as number)[01, 12]04
%MMinute[00, 59]00
%pAnte Meridiem or Post MeridiemAM, PMAM
%SSecond[00, 59]45
%UWeek (as number) – Sunday Start[00, 53]07
%wWeekday (as number, 0 = Sunday)[0, 6]5
%WWeek (as number) – Monday Start[00, 53]43
%xLocal Date RepresentationAny Date10/10/18
%XLocal Time RepresentationAny Time08:19:38
%yYear Modulo 100[00, 99]24
%YYearAny Year2008
%zTime Zone (Locale)Any Time ZoneCentral Standard Time
%ZTime Zone (Locale)Any Time ZoneCentral Standard Time
%%Percent Sign

%#cLong Date/Time Representation (Locale)Any Date and TimeSaturday, October 18, 2008 08:32:11
%#xLong Date Representation (Locale)Any DateSaturday, October 18, 2008

%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#YRemoves 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

strftime() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.