chefPeter
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 );
}
#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 );
}
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();
}
#include<dos.h>
#include<iostream.h>
#include <conio.h>
void main()
{
struct date d;
clrscr();
getdate(&d);
cout<<(int)d.da_day<<"/"<<
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 <<":"<<(in t)t.ti_min <<":"<<(in t)t.ti_sec <<":"<<(in t)t.ti_hun d;
getch();
}
#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<<"/"<<
gettime(&t);
cout<<endl<<(int)t.ti_hour
getch();
}
SYSTEMTIME time;
GetLocalTime(&time);
TCHAR s[20];
_stprintf(s, _T("%02d/%02d/%04d"),
time.wDay,
time.wMonth,
time.wYear);
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.
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
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 **
** Mindphaser - Community Support Moderator **
char acActualDate [ 128];
_strdate ( acActualDate );
printf ( "The Date is %s\n", acActualDate );