Last active
January 25, 2025 08:18
-
-
Save franklinjr12/80c099da840d685cd5048b9dcc4a4af9 to your computer and use it in GitHub Desktop.
C++ string to date (time_point)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <sstream> | |
#include <iomanip> | |
#include <format> | |
using namespace std; | |
using namespace std::chrono; | |
system_clock::time_point strToTp(string dateString) | |
{ | |
tm tm = {}; | |
stringstream ss(dateString); | |
ss >> get_time(&tm, "%d-%m-%Y"); | |
return system_clock::from_time_t(mktime(&tm)); | |
} | |
int main() | |
{ | |
// date in the format dd-mm-YYYY | |
string dateString = "29-08-2023"; | |
time_point tpFromStr = strToTp(dateString); | |
system_clock::time_point tpNow = system_clock::now(); | |
cout << format("dateString is older than now: {}\n", tpFromStr < tpNow ? "true" : "false"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment