-
Dates
Is there any better way of getting the system date values and storing them than this? The values have got to be stored as type int for the work i'll be doing.
Code:
#include <iostream.h>
#include <time.h>
int main()
{
const int DATE_LEN=5;
int Day, Month, Year;
static char Date[DATE_LEN];
time_t SystemDate;
SystemDate = time(NULL);
strftime(Date, sizeof(Date), "%d", localtime(&SystemDate));
Day = atoi(Date);
strftime(Date, sizeof(Date), "%m", localtime(&SystemDate));
Month = atoi(Date);
strftime(Date, sizeof(Date), "%Y", localtime(&SystemDate));
Year = atoi(Date);
cout << "The date is: Day " << Day << " Month " << Month << " Year " << Year;
cin.get();
return 0;
}
-
Would someone be able to comment on the code above, is there a more efficent way of storing the day, month and year? Or is this code ok?
-
Date.
Try this:
#include <dos.h>
#include <stdio.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
return 0;
}
-
Code:
#include <stdio.h>
#include <time.h>
int main ( ) {
time_t now;
struct tm tm_now;
now = time(NULL);
tm_now = *localtime(&now);
printf( "Day=%d, Month=%d, year=%d\n",
tm_now.tm_mday, tm_now.tm_mon+1, tm_now.tm_year+1900 );
return 0;
}
Shows you the tm fields containing the information you want
-
Thanks for your replies, the code is more efficient. Salem, just wondering if you could explain why you have to declare two structures to hold the information from the system clock, time_t and tm? Would be grateful for an explanation thanks.