Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Strings C++ Math C++ Booleans C++ Conditions C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Access Specifiers C++ Encapsulation C++ Inheritance C++ Polymorphism C++ Files C++ Exceptions C++ Date

C++ How To

C++ Add Two Numbers C++ Random Numbers

C++ Reference

C++ Reference C++ Keywords C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime>

C++ Examples

C++ Examples C++ Compiler C++ Exercises C++ Quiz C++ Certificate


C++ ctime strftime() Function

❮ ctime Functions


Example

Represent the current date in different ways:

time_t timestamp;
char output[50];
struct tm * datetime;

time(&timestamp);
datetime = localtime(&timestamp);

strftime(output, 50, "%B %e, %Y", datetime);
cout << output << "\n";

strftime(output, 50, "%I:%M:%S %p", datetime);
cout << output << "\n";

strftime(output, 50, "%D", datetime);
cout << output << "\n";

strftime(output, 50, "%c", datetime);
cout << output << "\n";
Try it Yourself »

Definition and Usage

The strftime() function writes a C-style string representation of a date and time (provided by a tm structure) into a char array. A format parameter specifies how the date and time are represented.

Note: Use the gmtime() or localtime() function to get a tm structure from a timestamp.

The format string

The format string is copied into the array with each of its format specifiers replaced by a generated value. The table below lists all of the format specifiers:

Format Specifier Description Example
%a Short representation of the weekday Fri
%A Full representation of the weekday Friday
%b Short representation of the month name Dec
%B Full representation of the month name December
%c Full date and time representation Fri Dec 17 14:30:01 2023
%C Century (equivalent to taking the first two digits of a 4-digit year) 20
%d Day of the month with leading zero 09
%D Date representation equivalent to %m/%d/%y 12/17/23
%e Day of the month with leading spaces  9
%F Date representation equivalent to %Y-%m-%d 2023-12-17
%g 2-digit week-based year (week-based years start at the beginning of a week) 23
%G 4-digit week-based year (week-based years start at the beginning of a week) 2023
%h Short representation of the month name (equivalent to %b) Dec
%H 24-hour format of an hour 14
%I 12-hour format of an hour 02
%j Day of the year (from 0 through 365) 351
%m Numeric representation of a month 351
%M Minutes within an hour 30
%n A \n new line character
%p AM or PM PM
%r Full 12-hour time format 02:30:01 PM
%R 24-hour time format equivalent to %H:%M 14:30
%S Seconds within a minute 01
%t A \t tab character
%T Full 24-hour time format equivalent to %H:%M:%S 14:30:01
%u Numeric representation of a day of the week (from 1 to 7 starting with Monday) 7
%U Week of the year starting at 0, with week 1 beginning on the first Sunday of the year 51
%V Week of the year starting at 1, with week 1 beginning on the first Monday of the year and any day in January before the first Monay belonging to the previous year 50
%w Numeric representation of a day of the week (from 0 to 6 starting with Sunday) 0
%W Week of the year starting at 0, with week 1 beginning on the first Monday of the year 50
%x Locale-based date representation 12/17/23
%X Locale-based time representation 14:30:01
%y 2-digit year representation 23
%Y 4-digit year representation 2023
%z Numeric timezone offset +0000
%Z Timezone name GMT
%% A % character %


Syntax

strftime(char * destination, size_t size, const char * format, const struct tm * datetime);

The size_t data type represents a non-negative integer.

Parameter Values

Parameter Description
destination Required. A char array in which to write the formatted date.
size Required. Specifies the amount of space available in the char array to write in, measured in characters.
format Required. Specifies how the date should be formatted.
datetime Required. A tm structure containing information about the date and time to be represented.

Technical Details

Returns: A number indicating how many characters were written to array. A return value of 0 indicates that there is not enough available space to write in.

❮ ctime Functions

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.