Skip to content

Instantly share code, notes, and snippets.

@franklinjr12
Last active January 25, 2025 08:18
Show Gist options
  • Save franklinjr12/80c099da840d685cd5048b9dcc4a4af9 to your computer and use it in GitHub Desktop.
Save franklinjr12/80c099da840d685cd5048b9dcc4a4af9 to your computer and use it in GitHub Desktop.
C++ string to date (time_point)
#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