C wcsftime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, wcsftime() formats date and time as a wide string. It belongs to the wide-character tools declared in <wchar.h>.

Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.

Syntax

The following concrete call shows the argument shape before the formal declaration.

C
wcsftime(out, 40, L"%Y-%m-%d", &timeinfo);
CallWhat it does
wcsftime(out, 40, L"%Y-%m-%d", &timeinfo)Formats date and time as a wide string in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
size_t wcsftime(wchar_t *restrict ws, size_t maxsize, const wchar_t *restrict format, const struct tm *restrict timeptr);

Parameters

ParameterDescription
wsA wide-string buffer, source string, or format string.
maxsizeCapacity of the destination wide-string buffer.
formatA wide format string that controls printing or scanning.
timeptrPointer to the broken-down calendar time to format.

Return Value

Return ValueDescription
size_t countThe number of wide characters produced or required; zero can mean the output did not fit.

C Examples

Example 1 — Format a date

This example uses wcsftime() to format a date, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>
#include <time.h>

int main(void) {
    wchar_t out[40]; struct tm t = {0};
    t.tm_year = 126; t.tm_mon = 10; t.tm_mday = 17;
    wcsftime(out, 40, L"%Y-%m-%d", &t);
    wprintf(L"%ls\n", out);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to format a date.
  2. Apply the wide-string operation. wcsftime(out, 40, L"%Y-%m-%d", &timeinfo) works in wchar_t elements and respects the ending L'\0'.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. 2026-11-17 confirms the behavior demonstrated by this example.

Example 2 — Choose format fields

This example uses wcsftime() to choose format fields, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>
#include <time.h>

int main(void) {
    wchar_t out[40]; struct tm t = {0};
    t.tm_year = 126; t.tm_mon = 10; t.tm_mday = 17;
    wcsftime(out, 40, L"%Y-%m-%d", &t);
    wprintf(L"%ls\n", out);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wcsftime() behaves for choose format fields.
  2. Apply the wide-string operation. wcsftime(out, 40, L"%Y-%m-%d", &timeinfo) works in wchar_t elements and respects the ending L'\0'.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. 2026-11-17 confirms the behavior demonstrated by this example.

Example 3 — Check buffer capacity

This example uses wcsftime() to check buffer capacity, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>
#include <time.h>

int main(void) {
    wchar_t out[40]; struct tm t = {0};
    t.tm_year = 126; t.tm_mon = 10; t.tm_mday = 17;
    wcsftime(out, 40, L"%Y-%m-%d", &t);
    wprintf(L"%ls\n", out);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to check buffer capacity.
  2. Apply the wide-string operation. wcsftime(out, 40, L"%Y-%m-%d", &timeinfo) works in wchar_t elements and respects the ending L'\0'.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. 2026-11-17 confirms the behavior demonstrated by this example.

Frequently Asked Questions

wcsftime() formats date and time as a wide string. It is part of the standard wide-character library.
Its parameters are ws, maxsize, format, timeptr. ws: A wide-string buffer, source string, or format string. maxsize: Capacity of the destination wide-string buffer. format: A wide format string that controls printing or scanning. timeptr: Pointer to the broken-down calendar time to format.
size_t count means the number of wide characters produced or required; zero can mean the output did not fit.
Include <wchar.h> for wcsftime(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
Inputs must be valid null-terminated wide strings unless the API explicitly uses a count. Writable destinations need room for the result and L'\0'.

Next: C wcslen() Function

Continue learning C wide-character functions.

Next tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful