0

I have taken in date from the user in DD/MM/YEAR formate but I want to compare it with system date and want to know whether the date entered by the user is in future or past !!

#include <iostream>
#include <ctime>

int main ()
{      
    char date [10],sysdate[10];
    cout<<"enter Date";           
    cin>>date;                   //for taking date from user
    _strdate(sysdate);      //for getting system date(given in DD/MM/YY    
    cout<< sysdate;        //prints system date    
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • If you're writing C++, it's not C. – Drew Dormann Jan 22 '15 at 18:31
  • The C++ `chrono` facility does not appear to provide any support for dealing with years, months, and days. You can use the C `mktime()` function. – jxh Jan 22 '15 at 18:41
  • It is often OS specific. On POSIX systems consider [strptime](http://man7.org/linux/man-pages/man3/strptime.3.html) & [strftime](http://man7.org/linux/man-pages/man3/strftime.3.html) etc.... – Basile Starynkevitch Jan 22 '15 at 18:57

2 Answers2

0

If you are dealing strictly with date strings (instead of datetime_ttimestamps or datetmstructures), then you can directly compare date strings of the format "YY/MM/DD" to determine whether one comes before or after the other.

The format you are using ("DD/MM/YY") does not allow this kind of direct string comparison.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52
0

Watch this example then it wont be hard to compare user date with system date

#include <iostream>
#include <ctime>

using namespace std;

int main( )
{
   // current date/time based on current system
   time_t now = time(0);


   tm *ltm = localtime(&now);

   // print various components of tm structure.
   cout << "Year: "<< 1900 + ltm->tm_year << endl;
   cout << "Month: "<< 1 + ltm->tm_mon<< endl;
   cout << "Day: "<<  ltm->tm_mday << endl;

  }
test program
  • 281
  • 1
  • 8