Article

The Portable Way to Extract Date and Time from a File's mtime in C++

Sandor Dargo ·Sandor Dargo's Blog ·Published 2026-07-22 ·5 min read

This article discusses a portable method for extracting date and time information from a file's modification time (mtime) in C++. It provides practical examples and code snippets to illustrate the approach.

From the article

After I finished my Time in C++ series , a reader asked a seemingly simple question:

“What is the portable way to extract the year, month, day, hour, minute, and second from a file’s modification time? I tried, and I couldn’t get it to work reliably on macOS and Windows.”

Fair question. You call std::filesystem::last_write_time() , you get back a time point, and you want the calendar components. How hard can it be?


Share this resource


Discovered 2026-08-02 Source Sandor Dargo's Blog Archive 2026-08 → c++datetimefilemtime