Getting Date and Time in C++11

You remember the days when it was necessary to use strftime to convert time to human readable strings? Even in C++ it actually was a pain in the ass.

This is the way how you can do in in C++11:

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);
    std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}

Hell yeah, I love C++11!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.