codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

13 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Date and Time
⚡C++ Programming

Date and Time

Updated 2026-05-12
30 min read

Date and Time

In this tutorial, you'll learn how to handle date and time in C++. Understanding how to work with dates and times is crucial for many applications, such as logging, scheduling tasks, or tracking events. C++ provides several libraries to help you manage date and time effectively.

Introduction

Date and time handling can be complex due to the variety of formats and the need for accurate calculations. In C++, the ctime library offers basic functionalities, while the more modern chrono library provides a more robust and flexible approach. This tutorial will cover both libraries, starting with the basics of the ctime library and then moving on to the chrono library.

Core Content

The ctime Library

The ctime library is part of the C++ Standard Library and provides functions for manipulating date and time. It includes types like time_t and structures like tm.

The time_t Type

time_t is an arithmetic type that represents calendar time as a long integer, which is typically measured in seconds since the Epoch (January 1, 1970).

basics.cpp
1#include <iostream>
2#include <ctime>
3
4int main() {
5 // Get the current time
6 std::time_t now = std::time(nullptr);
7
8 // Print the current time as a long integer
9 std::cout << "Current time: " << now << std::endl;
10
11 return 0;
12}
Output
Current time: 1672531200

The tm Structure

The tm structure is used to hold the broken-down time, which includes components like year, month, day, hour, minute, and second.

tm_structure.cpp
1#include <iostream>
2#include <ctime>
3
4int main() {
5 // Get the current time
6 std::time_t now = std::time(nullptr);
7
8 // Convert to local time
9 std::tm* localTime = std::localtime(&now);
10
11 // Print individual components of the time
12 std::cout << "Year: " << localTime->tm_year + 1900 << std::endl;
13 std::cout << "Month: " << localTime->tm_mon + 1 << std::endl;
14 std::cout << "Day: " << localTime->tm_mday << std::endl;
15 std::cout << "Hour: " << localTime->tm_hour << std::endl;
16 std::cout << "Minute: " << localTime->tm_min << std::endl;
17 std::cout << "Second: " << localTime->tm_sec << std::endl;
18
19 return 0;
20}
Output
Year: 2023
Month: 1
Day: 1
Hour: 0
Minute: 0
Second: 0

Formatting Date and Time with strftime

The strftime function allows you to format date and time into strings according to your specifications.

strftime.cpp
1#include <iostream>
2#include <ctime>
3
4int main() {
5 // Get the current time
6 std::time_t now = std::time(nullptr);
7
8 // Convert to local time
9 std::tm* localTime = std::localtime(&now);
10
11 // Buffer to hold formatted date and time string
12 char buffer[80];
13
14 // Format the date and time
15 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
16
17 // Print the formatted date and time
18 std::cout << "Formatted Date and Time: " << buffer << std::endl;
19
20 return 0;
21}
Output
Formatted Date and Time: 2023-01-01 00:00:00

The chrono Library

The chrono library, introduced in C++11, provides a more modern and flexible approach to date and time handling. It includes types like duration, time_point, and clocks.

Basic Usage of chrono

chrono_basic.cpp
1#include <iostream>
2#include <chrono>
3
4int main() {
5 // Get the current time
6 auto now = std::chrono::system_clock::now();
7
8 // Convert to time_t
9 std::time_t now_c = std::chrono::system_clock::to_time_t(now);
10
11 // Print the current time as a string
12 std::cout << "Current time: " << std::ctime(&now_c) << std::endl;
13
14 return 0;
15}
Output
Current time: Sun Jan 1 00:00:00 2023

Measuring Time with chrono

You can use chrono to measure the duration of operations.

chrono_duration.cpp
1#include <iostream>
2#include <chrono>
3#include <thread>
4
5int main() {
6 // Start time
7 auto start = std::chrono::high_resolution_clock::now();
8
9 // Simulate some work with a sleep
10 std::this_thread::sleep_for(std::chrono::seconds(1));
11
12 // End time
13 auto end = std::chrono::high_resolution_clock::now();
14
15 // Calculate duration
16 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
17
18 // Print the duration
19 std::cout << "Duration: " << duration.count() << " milliseconds" << std::endl;
20
21 return 0;
22}
Output
Duration: 1001 milliseconds

Practical Example

Let's create a simple program that logs the current date and time to a file every second for 10 seconds.

logger.cpp
1#include <iostream>
2#include <fstream>
3#include <ctime>
4#include <chrono>
5#include <thread>
6
7int main() {
8 std::ofstream logFile("log.txt");
9
10 if (!logFile.is_open()) {
11 std::cerr << "Failed to open log file." << std::endl;
12 return 1;
13 }
14
15 for (int i = 0; i < 10; ++i) {
16 // Get the current time
17 auto now = std::chrono::system_clock::now();
18
19 // Convert to time_t
20 std::time_t now_c = std::chrono::system_clock::to_time_t(now);
21
22 // Format the date and time
23 char buffer[80];
24 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&now_c));
25
26 // Log to file
27 logFile << "Log entry: " << buffer << std::endl;
28
29 // Sleep for 1 second
30 std::this_thread::sleep_for(std::chrono::seconds(1));
31 }
32
33 logFile.close();
34 std::cout << "Logging complete." << std::endl;
35
36 return 0;
37}

Summary

ConceptDescription
time_tRepresents calendar time as a long integer.
tmStructure to hold broken-down time components.
strftimeFormats date and time into strings according to specifications.
chronoModern library for date and time handling, including duration and time_point.

What's Next?

Now that you have a good understanding of date and time handling in C++, the next step is to learn about conditional statements like if, if...else, and nested if...else. These will allow you to make decisions based on conditions, which is essential for controlling the flow of your programs.

Stay tuned for our next tutorial where we'll dive into these important control structures!


PreviousMath & NumbersNext if, if...else, and Nested if...else

Recommended Gear

Math & Numbersif, if...else, and Nested if...else