C Language | Date and Time Inbuilt Functions

To fetch the system date and display.

In order to fetch date from the system we use a system defined structure named “date”.

Function used :

getdate():

This function helps us to fetch system date and store in a date type variable
header file: dos.h

setdate():

This function helps us to modify the system date by the date stored in a date type variable.
header file: dos.h

Structure of the date as used by these functions:
getdate()
setdate()

struct date {
int da_year; current year
char da_day; day of the month
char da_mon; month (1 = Jan)
};

/* Program developed in Turboc3 */
/* getdate() Gets DOS system date
header file: dos.h */
/*
Structure of the date as used by these functions:
  getdate()
  setdate()

  struct date {
    int da_year;     current year
    char da_day;     day of the month
    char da_mon;     month (1 = Jan)
  };
  */
#include <dos.h>
#include <stdio.h>
#include<conio.h>
void main()
{
   struct date d;
   getdate(&d);
   clrscr();
   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);

   printf("date is : %d-%d-%d\n",d.da_day,d.da_mon,
   d.da_year);
getch();
}

Program to show system date and then asks the user to change the date from within the program. Then display the changed date.

/* Program developed in Turboc3 */
/*
Program to display System Date and
allow the user to modify the System Date
*/
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{
   struct date d1,d2;
   int d,m,y;
   clrscr();
   getdate(&d1);
   printf("date is (dd-mm-yy) : %d-%d-%d\n",
		d1.da_day,d1.da_mon,d1.da_year);

   printf("Enter new date\n");
   printf("Enter day :");scanf("%d",&d);
   printf("Enter month : ");scanf("%d",&m);
   printf("Enter year : ");scanf("%d",&y);

   d2.da_year = y;
   d2.da_day = d;
   d2.da_mon = m;
   setdate(&d2);
   printf("Date after setting:\n");
   getdate(&d1);
   printf("date is (dd-mm-yy) : %d-%d-%d\n",
		d1.da_day,d1.da_mon,d1.da_year);
   getch();
}

Program to display system date and time
and program to calculate sum of two numbers(Any Program)

/* Program developed in Turboc3 */
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void show_date_time()
{
   struct  time t;
   struct date d1;
   getdate(&d1);
   gotoxy(10,3);
   printf("Todays date is (dd-mm-yy) : %d-%d-%d\n",
		d1.da_day,d1.da_mon,d1.da_year);
   gotoxy(10,4);
   gettime(&t);
   printf("Todays time is:%2d:%02d:%02d.%02d\n",
	t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
   printf("\n\n\n");
}
void sum()
{
  int a,b,c;
   printf("Enter 2 numbers ");
   scanf("%d %d",&a,&b);
   c=a+b;
   printf("Sum = %d\n",c);
}
void main()
{
  clrscr();
  show_date_time();
  sum();
  getch();
}

C Language Programming Tutorial

C Language Tutorial Home    Introduction to C Language     Tokens     If Condition     goto statement and Labelname     Switch Statements     For loop     While Loop    Do while loop     break and continue    Functions    Recursion    Inbuild Functions    Storage Classes    Preprocessor    Arrays    Pointers     Structures and Unions    File Handling     Projects