C library function -strftime()

C Standard Library - <time.h>

Description

C Library Functionssize_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)According toformatThe formatting rules and formatting structure defined intimeptrthe time represented, and store it instrIn.

Declaration

Below is the declaration of the strftime() function.

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

Parameter

  • str-- This is a pointer to the destination array, used to copy the resulting C string.
  • maxsize-- This is the maximum number of characters to be copied to str.
  • format-- This is a C string containing any combination of ordinary characters and special format specifiers. These format specifiers are replaced by the function with corresponding values representing the time specified in tm. The format specifiers are:
SpecifierReplace withExample
%aAbbreviated weekday nameSun
%AFull weekday nameSunday
%bAbbreviated month nameMar
%BFull month nameMarch
%cDate and time representationSun Aug 19 02:56:02 2012
%dDay of the month (01-31)19
%HHour in 24-hour format (00-23)14
%IHour in 12-hour format (01-12)05
%jDay of the year (001-366)231
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%pAM or PM namePM
%SSecond (00-61)02
%UWeek of the year, with the first Sunday as the first day of the first week (00-53)33
%wDay of the week as a decimal number, Sunday represented as 0 (0-6)4
%WWeek of the year, with the first Monday as the first day of the first week (00-53)34
%xDate representation08/19/12
%XTime representation02:50:06
%yYear, last two digits (00-99)01
%YYear2012
%ZTime zone name or abbreviationCDT
%%A % sign%
  • timeptr-- This is a pointer to a tm structure that contains a calendar time broken down into the following parts:
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59                */
   int tm_min;         /* 分,范围从 0 到 59                */
   int tm_hour;        /* 小时,范围从 0 到 23                */
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31                    */
   int tm_mon;         /* 月份,范围从 0 到 11                */
   int tm_year;        /* 自 1900 起的年数                */
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6                */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365                    */
   int tm_isdst;       /* 夏令时                        */    
};

Return Value

If the resulting C string is less than size characters (including the null terminating character), the total number of characters copied to str is returned (excluding the null terminating character); otherwise, zero is returned.

Example

The following example demonstrates the usage of the strftime() function.

Example

#include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info); printf("Formatted date & time : |%s|\n", buffer ); return(0); }

Let us compile and run the above program, which will produce the following result:

格式化的日期 & 时间 : |2018-09-19 08:59:07|

C Standard Library - <time.h>

other extensions