How to get current date & time in our C & C++ programs ?

 

If we want to display current date & time in our C & C++ project then we may use getdate() & gettime() functions of <dos.h> header file.

We get date by following these 3 steps :

1). Declare a struct date variable :

struct date d;

2).Pass this struct variable to getdate function :

getdate(&d);

3).Print or store current year,month,day using structure members :

d.da_year , d.da_mon , d.da_day

Example :

#include <stdio.h>
#include <dos.h>
#include <conio.h>
void main()
{
struct date d;
clrscr();
getdate(&d);

printf(“The current day is: %dn”, d.da_day);
printf(“The current month is: %dn”, d.da_mon);
printf(“The current year is: %dn”, d.da_year);

getch();
}

 

/*——————————————————————–*/

For getting current time :

1). Declare a struct time variable :

struct time t;

2).Pass this struct variable to gettime function :

gettime(&t);

3).Print or store current hours,minutes,seconds & hundreds using structure members :

t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund

Example :

#include <stdio.h>
#include <dos.h>
#include <conio.h>
void main()
{
struct time t;
clrscr();
gettime(&t);
printf(“The current time is: %2d:%02d:%02d.%02dn”,
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
getch();
}


/*—————————————————————————————*/

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *