0

i have that c++ project. i want to get today's date to compare it with a saved date in my files. i already searched but all i found is i can output it on the console but that isn't what i want. is it possible?

#include <iostream>
#include <cmath>
#include <string>
#include <chrono>
#include <ctime>
using namespace std;
static  double interset = .05;
class Account{
public:
    string ID;
    double Balance;
    void Deposit(double bal){
        Balance += bal;
    }
    void Withdraw(double bal){
        if (bal > Balance){
            cout << "Please check the entered amount" << endl;
        }
        else{
            Balance -= bal;
        }
     }
    void BalanceInqu(){
        cout << "Your Current Balance Is\t" << Balance << endl;
    }
 };

class SavingAccount : public Account{
public:
    void intersetRate(){
         \\i want to put here a function that calculates the interest rate of an client depending on his account creation date
    }
};

Edit: i want to get the date to store it into variable and compare it with other dates

  • Possible duplicate of [How to get current time and date in C++?](https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c) – hnefatl Apr 16 '18 at 22:57
  • i don't want to output it i want to store it into variable – Maher Farghaly Apr 16 '18 at 23:00
  • 1
    in order to output the time, those answers first store the current date-time in a variable of type [`std::time_t`](http://en.cppreference.com/w/cpp/chrono/c/time_t) (an arithmetic type) and then into a [`std::tm`](http://en.cppreference.com/w/cpp/chrono/c/tm) (a structured type), they are applicable even if they print the result – kmdreko Apr 16 '18 at 23:09
  • @vu1p3n0x thankyou i couldn't realize that without you making it clear. – Maher Farghaly Apr 16 '18 at 23:19

2 Answers2

0

If you have the date or time for now, you just need to subtract the start time and the transform the result into days, if that's not already the case. So this means that you don't need to get the actual date for today.

The time_since_epoch can be enough, because you just want to have the difference between 2 timestamps.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
flyingdutchman
  • 1,197
  • 11
  • 17
0
#include <iostream>
#include <chrono>
#include <ctime>

tm chronoTPtoTM(const std::chrono::system_clock::time_point& tp) {
    time_t aux = std::chrono::system_clock::to_time_t(tp);
    return *localtime(&aux);
}

int main(int argc, char** argv) {
    std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
    tm local_time = chronoTPtoTM(t);

    std::cout   << "Now is "
            << local_time.tm_year+1900 << "/"
            << local_time.tm_mon+1 << "/"
            << local_time.tm_mday << " "
            << local_time.tm_hour << "h"
            << local_time.tm_min << "m"
            << local_time.tm_sec << "s"
            << std::endl;
    return 0;
}

This is a simple working example of the usage of the std::chrono::system_clock::time_point. This class even has comparator operators defined so you can compare two of these easily with <, >, <=, >=, "==" and "!=".

In the example, I've included a way to convert the time_point into a human readable format.

Luis Paulo
  • 421
  • 5
  • 10