Link to home
Start Free TrialLog in
Avatar of chefPeter
chefPeterFlag for Sweden

asked on

get system date

How can I get system date in(dd/mm/yyyy) format in C++. I created my application as win32 console application. Please show me the code!!! Thank u
ASKER CERTIFIED SOLUTION
Avatar of EOL
EOL

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
I'd use '_strdate()':

char acActualDate [ 128];

_strdate ( acActualDate );

printf ( "The Date is %s\n", acActualDate );
or you can use tm struct

#include <stdio.h>
#include <string.h>
#include <time.h>

void main( void )
{
        struct tm *newtime;
        char am_pm[] = "AM";
        time_t long_time;

        time( &long_time );                /* Get time as long integer. */
        newtime = localtime( &long_time ); /* Convert to local time. */

        if( newtime->tm_hour > 12 )        /* Set up extension. */
                strcpy( am_pm, "PM" );
        if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
                newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
        if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight. */
                newtime->tm_hour = 12;

        printf( "%.19s %s\n", asctime( newtime ), am_pm );
}
Avatar of aneelbaig
aneelbaig

Use the following code.  I think it is more easy to understand and very precise too.

#include<dos.h>
#include<iostream.h>
#include <conio.h>
void main()
 {
   struct date d;
   clrscr();
   getdate(&d);
  cout<<(int)d.da_day<<"/"<<(int)d.da_mon<<"/"<<(int)d.da_year;
  getch();
 }
 
And if want both time and date to be displayed then




#include<dos.h>
#include<iostream.h>
#include <conio.h>
void main()
 {
   struct date d;
   struct time t;
   clrscr();
   getdate(&d);
  cout<<(int)d.da_day<<"/"<<(int)d.da_mon<<"/"<<(int)d.da_year;
  gettime(&t);
  cout<<endl<<(int)t.ti_hour<<":"<<(int)t.ti_min<<":"<<(int)t.ti_sec<<":"<<(int)t.ti_hund;
  getch();
 }
SYSTEMTIME time;
GetLocalTime(&time);

TCHAR s[20];
_stprintf(s, _T("%02d/%02d/%04d"),
    time.wDay,
    time.wMonth,
    time.wYear);
Weeee, I'm baffeld how many different methods there are to get the time :), and nobody gets tired to repeat them all.
lol eol, you've got a point but [ctrl]+[c] isn't too dificult.

But AlexFM's solution is poor: the function _stprintf() is not portable. But seeing as how chefPeter is using cpp he probabbly will use

cout << time.wDay;

et cetera.
Bah, I don't bother, one thread more of the 100th, doesn't make a difference.
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "EOL"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **