I am trying to get the system date into 3 different varibles Month,Day, and Year but I cant figure out how to get the system date from the OS.

There's a header called time.h (ctime in C++) that contains the functions and types you need. Try a google search for details, and if you have trouble with the actual implementation, feel free to ask here for help.

This is what I have for the code to get the system date and put it in 3 variables, is this the best way to do this?

void main()

{
  
    struct tm *Sys_T = NULL;                     
  
    time_t Tval = 0;                            
    Tval = time(NULL);                          
    Sys_T = localtime(&Tval);                   

     Day=Sys_T->tm_mday;
    Month=Sys_T->tm_mon+1;
    Year=1900 + Sys_T->tm_year;

    printf("\nMonth = %d", Month);
    printf("\nDay   = %6.6f", Day);
    printf("\nYear  = %d", Year);
    printf("\n\n\n");

    printf("Hit any key to exit ! \n\n");
    
    getch();

}

>>is this the best way to do this?

Yes, that's how all the programs I've seen does it, although there are other os-specific functions. The code you wrote is completely portable and correct.

I'm clueless as to why the programmer decided to use %6.6f instead of %d in the second printf statement. I'm just as clueless about why the programmer used void main.

I'm clueless as to why the programmer decided to use %6.6f instead of %d in the second printf statement. I'm just as clueless about why the programmer used void main.

I did not look at that part of the code -- but you are right.:)

The Month and the Year are integer variables. I used a double varible for the day because later in the program I will be adding the fraction of the day it. I need to have 6 digits of precision later in the program. That was the only reason I used %6.6f for the output in that statement.

For this example you are right I should have used a %d. Thanks for looking at this for me.

This article has been dead for over six months. Start a new discussion instead.