How to get the current system and date time in C++

2 Answers

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

std::string systemTimeToString(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 << systemTimeToString(current_time);

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

 



answered May 16, 2021 by avibootz
0 votes
#include <iostream>
#include <ctime>

int main()
{
    time_t tm = time(0);
    char *dt = ctime(&tm);
    
    std::cout << dt;
    
    return 0;
}




/*
run:
   
Fri Sep 17 09:30:06 2021
   
*/

 



answered Sep 17, 2021 by avibootz
...