How to convert the current system date and time to string in C++

1 Answer

0 votes
#include <iostream>
#include <chrono>

std::string systemDateTimeToString(std::chrono::system_clock::time_point &t) {
    std::time_t time = std::chrono::system_clock::to_time_t(t);
    
    std::string s = std::ctime(&time);

    return s;
}

int main() {
    auto current_time = std::chrono::system_clock::now();
    
    std::cout << systemDateTimeToString(current_time);

    return 0;
}
 
 
 
 
/*
run:
 
Sun May 16 09:49:44 2021
 
*/

 



answered May 16, 2021 by avibootz
...