Search code examples
c++visual-studio-2022fmt

C++ namespace "std" has no member "format" despite #include <format>


I am new to C++. I am trying to store the current date and time as a string variable.

At this question, I found an answer, and installed the date.h library.

However, when I try to use the code provided, I am met with the error:

namespace "std" has no member "format"

Despite having #include <format> at the top of the script.

How can I fix this?

I am using Visual Studio 2022 on Windows 10, if that helps.

Here is my code:

#include <iostream>
#include <chrono>
#include <date.h>
#include <type_traits>
#include <format>


int main()
{
    std::cout << "The current time is ";
    auto start_time = std::format("{:%F %T}", std::chrono::system_clock::now());
    static_assert(std::is_same_v<decltype(start_time), std::string>{});
    std::cout << start_time << "\n";
}

Solution

  • std::format was added to C++ in the C++20 standard. Unless you compile with C++20, you won't have std::format.