
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C Library - wcsftime() function
The C Library wcsftime() function transform the date and time information into wide character string according to specified format. While the printing the date, it use the function wprintf(). This function is used for formatted output with wide characters. It allows users to print a series of wide character and values as standard output.
Syntax
Following is the syntax of the C Library wcsftime() function −
wcsftime(buffer, int_value, L"The current Date: %A, %F %I:%M %p", timeinfo); and wprintf(L"%ls\n", buffer);
Parameters
This function accepts the following parameters −
1. buffer: A pointer to the first element of the wide character array where the output will be stored.
2. int_valuie: The maximum number of wide characters to write (size of the buffer).
3. format: A pointer to a null-terminated wide character string specifying the format of conversion and following are the list of common conversion specifiers that used in date and time formatting−
- %A: Full weekday name (e.g., "Sunday/Monday/Tuesday etc.").
- %F: Full date in the format "YYYY-MM-DD"(e.g., "2024-05-22").
- %H: Hour (24-hour clock) as a decimal number(e.g., "19").
- %M: Minute as a decimal number (e.g., "30").
- %I:%M %p: Hour (12-hour clock) and minute, followed by AM/PM indicator.
- %S: Second as a decimal number (e.g., "45").
4. timeinfo: A pointer to a struct tm containing the local time information.
Return Type
On program succession, the function returns the total number of wide characters which copied to string and excluding the terminating null wide character. If the output exceeds maximum size, the function returns zero.
Example 1
Following is the basic C Library program to display the current date and time in a custom format using wcsftime() function.
#include <stdio.h> #include <wchar.h> #include <time.h> int main() { time_t demotime; struct tm* timeinfo; wchar_t buffer[80]; time(&demotime); timeinfo = localtime(&demotime); // Custom formatting of time wcsftime(buffer, 80, L"The current Date: %A, %F %I:%M %p", timeinfo); wprintf(L"%ls\n", buffer); return 0; }
Output
On execution of above code, we get the following output −
The current Date: Wednesday, 2024-05-22 01:38 PM
Example 2
Below the example utilize the localtime formatting in 24-hours clock using the various function.
#include <stdio.h> #include <wchar.h> #include <locale.h> #include <time.h> int main() { time_t rawtime; struct tm* timeinfo; wchar_t buffer[80]; time(&rawtime); timeinfo = localtime(&rawtime); // Set the locale category setlocale(LC_ALL, "ja_JP.utf8"); // Custom Formatting to 24-hour clock wcsftime(buffer, 80, L": %A%F %H:%M", timeinfo); // Display the result wprintf(L"%ls\n", buffer); return 0; }
Output
After executing the code, we get the following output −
?????: Wednesday?2024-05-22 13:42
Example 3
To obtain the today's date the wcsftime() format set the custom setting of conversion specifier and display the result.
#include <stdio.h> #include <time.h> #include <wchar.h> int main(void) { struct tm* timeinfo; wchar_t dest[100]; time_t temp; size_t r; temp = time(NULL); timeinfo = localtime(&temp); r = wcsftime(dest, sizeof(dest), L" Today is %A, %b %d.\n Time: %I:%M %p", timeinfo); printf("%zu is the placed characters in the string:\n\n%ls\n", r, dest); return 0; }
Output
The above code produces the following output −
44 The placed characters in the string: Today is Wednesday, May 22. Time: 02:28 PM