C Time Functions - C strftime






Format time as string

Prototype

size_t strftime (char* ptr, size_t maxsize, const char* format,
                 const struct tm* timeptr ); 

Parameter

This function has the following parameter.

ptr
Pointer to the destination array for the resulting string.
maxsize
Maximum number of characters to be copied to ptr, including the terminating null-character.
format
Format stringThey all begin with a percentage % sign, and are:
specifierReplaced byExample
%aAbbreviated weekday nameThu
%AFull weekday name Thursday
%bAbbreviated month nameAug
%BFull month name August
%cDate and time representation Thu Jan 22 14:55:02 2015
%CYear divided by 100 and truncated to integer (00-99)20
%dDay of the month, zero-padded (01-31)22
%DShort MM/DD/YY date, equivalent to %m/%d/%y08/22/10
%eDay of the month, space-padded ( 1-31)22
%FShort YYYY-MM-DD date, equivalent to %Y-%m-%d2015-08-22
%gWeek-based year, last two digits (00-99)01
%GWeek-based year2015
%hAbbreviated month name * (same as %b)Aug
%HHour in 24h format (00-22)14
%IHour in 12h format (01-12)02
%jDay of the year (001-366)225
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%nNew-line character ('\n')
%pAM or PM designationPM
%r12-hour clock time *02:55:02 pm
%R24-hour HH:MM time, equivalent to %H:%M14:55
%SSecond (00-61)02
%tHorizontal-tab character ('\t')
%TISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S14:55:02
%uISO 8601 weekday as number with Monday as 1 (1-7)4
%UWeek number with the first Sunday as the first day of week one (00-53)33
%VISO 8601 week number (00-53)34
%wWeekday as a decimal number with Sunday as 0 (0-6)4
%WWeek number with the first Monday as the first day of week one (00-53)34
%xDate representation *08/22/01
%XTime representation *14:55:02
%yYear, last two digits (00-99)01
%YYear2015
%zISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)+100
%ZTimezone name or abbreviationCDT
%%A % sign%
timeptr
Pointer to a tm structure.

Return

The function returns the total number of characters copied to ptr on success.

Otherwise, it returns zero.

Example


#include <stdio.h> /* puts */
#include <time.h> /* time_t, struct tm, time, localtime, strftime */
//from   w ww.j av  a 2 s. com
int main (){
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
  puts (buffer);

  return 0;
} 

The code above generates the following result.





Example 2


#include <stdio.h>
#include <time.h>
#include <locale.h>
 /*from  www  . j  av  a2  s .c o  m*/
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");
    }
}

The code above generates the following result.