Methods to Get Time Format in C++

std::string getCurrentDate() {    // Get current time    auto now = std::chrono::system_clock::now();    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    // Use localtime_s instead of localtime    std::tm now_tm;    localtime_s(&now_tm, &now_time);
    // Manually format    char buffer[11]; // YYYY-MM-DD + null terminator    std::snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d",        now_tm.tm_year + 1900,  // Year starts from 1900        now_tm.tm_mon + 1,      // Month starts from 0        now_tm.tm_mday);        // Day starts from 1
    return std::string(buffer);}

Leave a Comment