Format time as string
size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
This function has the following parameter.
specifier | Replaced by | Example |
---|---|---|
%a | Abbreviated weekday name | Thu |
%A | Full weekday name | Thursday |
%b | Abbreviated month name | Aug |
%B | Full month name | August |
%c | Date and time representation | Thu Jan 22 14:55:02 2015 |
%C | Year divided by 100 and truncated to integer (00-99 ) | 20 |
%d | Day of the month, zero-padded (01-31 ) | 22 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 08/22/10 |
%e | Day of the month, space-padded ( 1-31 ) | 22 |
%F | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2015-08-22 |
%g | Week-based year, last two digits (00-99 ) | 01 |
%G | Week-based year | 2015 |
%h | Abbreviated month name * (same as %b ) | Aug |
%H | Hour in 24h format (00-22 ) | 14 |
%I | Hour in 12h format (01-12 ) | 02 |
%j | Day of the year (001-366 ) | 225 |
%m | Month as a decimal number (01-12 ) | 08 |
%M | Minute (00-59 ) | 55 |
%n | New-line character ('\n' ) | |
%p | AM or PM designation | PM |
%r | 12-hour clock time * | 02:55:02 pm |
%R | 24-hour HH:MM time, equivalent to %H:%M | 14:55 |
%S | Second (00-61 ) | 02 |
%t | Horizontal-tab character ('\t' ) | |
%T | ISO 8601 time format (HH:MM:SS ), equivalent to %H:%M:%S | 14:55:02 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7 ) | 4 |
%U | Week number with the first Sunday as the first day of week one (00-53 ) | 33 |
%V | ISO 8601 week number (00-53 ) | 34 |
%w | Weekday as a decimal number with Sunday as 0 (0-6 ) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53 ) | 34 |
%x | Date representation * | 08/22/01 |
%X | Time representation * | 14:55:02 |
%y | Year, last two digits (00-99 ) | 01 |
%Y | Year | 2015 |
%z | ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) | +100 |
%Z | Timezone name or abbreviation | CDT |
%% | A % sign | % |
The function returns the total number of characters copied to ptr on success.
Otherwise, it returns zero.
#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.
#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.