+92 332 4229 857 99ProjectIdeas@Gmail.com

How to get the current date (C++)


How to get the current date
The following code shows how to get current date using time library.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "time.h"

using namespace std;

int main()
{
       struct tm *ptr;   
       time_t sec;       
       time(&sec);       
       ptr=localtime(&sec);

       int month = (short) ptr->tm_mon + 1;
       int day   = (short) ptr->tm_mday;
       int year  = (short) ptr->tm_year + 1900;

       cout<<"Current date in MM//DD//YY format is > "<<month<<"/"<<day<<"/"<<year<<endl;

       _getche();
       return 0;
}
Output
Current date in MM//DD//YY format is > 2/14/2011

0 comments: