![]() |
VOOZH | about |
In C++, the function std::get_time() is a standard library function that is used to parse the given input as date and time value as specified in the format string that is passed as the argument. It stores that parsed time in the object of the tm type.
std::get_time() is defined inside <ctime> header in C++.
get_time(tm* var, const CharT* format);This function accepts two parameters:
The format string for the get_time() has its own set of rules for parsing the date/time data from the given input. These rules states different specifiers to parse the date/time in different format. The following table list some of the common specifiers:
| Specifier | Description |
|---|---|
| %a | Abbreviated weekday (e.g., Mon, Tue) |
| %A | Full weekday (e.g., Monday, Tuesday) |
| %b | Abbreviated month (e.g., Jan, Feb) |
| %B | Full month (e.g., January, February) |
| %c | Day of the month with weekday (e.g., 02 Mon) (Less common) |
| %C | Count of the weekday within the year (range 00 to 53) |
| %d | Day of the month, 2 digits (range 00 to 31) |
| %D | Day of the year, 3 digits (range 000 to 366) |
| %F | Equivalent to %Y-%m-%d (ymd's canonical format) |
| %g | ISO week date year without the century (range 00 to 99) |
| %G | ISO week date year including the century |
| %j | Equivalent to %D |
| %m | Month as a number (01 to 12) |
| %M | Minute (00 to 59) |
| %q | Quarter (01 to 04) (Less common) |
| %s | Seconds since January 1, 1970 (for timestamps) |
| %u | Day of the week (number, Sunday = 1) |
| %U | Week count, day of week is Sun (range 00 to 53) |
| %V | ISO week count, day of week is Mon (range 01 to 53) |
| %w | Day of the week (number, Sunday = 0) |
| %W | Week count, day of week is Mon (range 00 to 53) |
| %y | Year without a century (range 00 to 99) |
| %Y | Year including the century |
| %Z | Zone offset in hours and minutes (HH:MM) with a preceding sign (+ for offsets east of UTC, - for offsets west of UTC) |
Please refer to the documentation for more specifiers.
This function does not return any value after its execution. It just stores the input data into the given argument.
The below program demonstrates how to use the get_time() function in C++:
Year: 2000 Month: 1 Day: 1 Hour: 0 Minute: 0 Second: 0
strptime().